Regex to match mm/dd/yyyy hh:mm:ss AM or PM

10,395

Solution 1

This regex should work for you for validation of your inputs:

^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} [AP]M\z

RegEx Demo

This will validate either of these inputs lines:

10/22/2016 9:15:32 PM
9/9/2016 9:9:5 AM

Solution 2

I don't think a regex is the right tool for this job. I'd use DateTime.strptime, give it the format you're expecting and let it deal with all the "50 hours is invalid in a time of day", "09 versus 9", ... issues.

Something like:

require 'date'

def timestamp
  print 'Enter the time stamp to verify: '
  ts = gets.chomp # You might want to include a String#strip call in here too
  DateTime.strptime(ts, '%m/%d/%Y %I:%M:%S %p')
  puts ts
rescue ArgumentError
  puts 'Invalid formatting of time stamp'
  exit 1
end

Regexes are nice but they shouldn't be the only parsing tool you have.

Solution 3

Characters like / and :, and even letters can be added litterally. Try this:

/[01][0-9]/[0-3][0-9]/[0-9]{4} [0-2]?[0-9]:[0-6]?[0-9]:[0-6]?[0-9] (AM|PM)/

Solution 4

Try this . I hope i understood your question. Tested here http://rubular.com/ and it works.

  ^(\d{1,2}\/){2}\d{4}\s+((\d+)(\:)){2}\d+\s+(AM|PM)

enter image description here

Solution 5

Like stated by mu is too short, regex is great and all but you need more in your arsenal then just that.

def timestamp
  print 'Enter time stamp: '
  ts = gets.chomp.upcase
  verify = DateTime.strptime(ts, '%m/%d/%Y %I:%M:%S %p')
  puts ts
rescue ArgumentError
  puts 'Invalid timestamp formatting must contain date time AM or PM'
  timestamp
end

Of course this could cause an overflow from the buffer because it's recursive, but that chances of that happening are pretty slim.

When this is run:

irb(main):01:0> def timestamp
irb(main):02:1>   print 'Enter time stamp: '
irb(main):02:1>   ts = gets.chomp.upcase
irb(main):04:1>   verify = DateTime.strptime(ts, '%m/%d/%Y %I:%M:%S %p')
irb(main):05:1>   puts ts
irb(main):06:1> rescue ArgumentError
<valid timestamp formatting must contain date time AM or PM'
irb(main):07:1>   timestamp
irb(main):08:1> end
=> :timestamp
irb(main):09:0> timestamp
Enter time stamp: test
Invalid timestamp formatting must contain date time AM or PM
Enter time stamp: 06/30/2016 6:6:54 pm
06/30/2016 6:6:54 PM
=> nil

However it is possible that you could make a better way of checking:

def enter_stamp
  print 'Enter timestamp: '
  ts = gets.chomp
  verify_stamp(ts)
end

def verify_stamp(timestamp)
  Date.strptime(timestamp, '%m/%d/%Y %I:%M:%S %p')
  puts timestamp unless ArgumentError
rescue ArgumentError
  puts 'Invalid timestamp format'
  enter_stamp
end

This makes it so that the methods are more OOPish, and Ruby is all about OOP so it kind of works. When this is run:

irb(main):001:0> require 'date'
=> true
irb(main):002:0>
irb(main):003:0* def enter_stamp
irb(main):004:1>   print 'Enter timestamp: '
irb(main):005:1>   ts = gets.chomp
irb(main):006:1>   verify_stamp(ts)
irb(main):007:1> end
=> :enter_stamp
irb(main):008:0>
irb(main):009:0* def verify_stamp(timestamp)
irb(main):010:1>   Date.strptime(timestamp, '%m/%d/%Y %I:%M:%S %p')
irb(main):011:1>   puts timestamp unless ArgumentError
irb(main):012:1> rescue ArgumentError
irb(main):013:1>   puts 'Invalid timestamp format'
irb(main):014:1>   enter_stamp
irb(main):015:1> end
=> :verify_stamp
irb(main):016:0> enter_stamp
Enter timestamp: test
Invalid timestamp format
Enter timestamp: 06/12/2016 8:08:54 pm
=> nil

And of course you could also use just a regex:

def verify_timestamp(stamp)
  if !(stamp[/^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} [AP]M\z/])
    puts 'Invalid timestamp formatting'
    timestamp
  else
    stamp
  end
end

def timestamp
  print 'Enter timestamp for removal: '
  ts = gets.chomp
  verify_timestamp(ts)
end

As I said regexs are wonderful, they just aren't exactly the best thing to use all the time.. When this is run:

irb(main):001:0> def verify_timestamp(stamp)
<mp[/^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} [AP]M\z/])
irb(main):003:2>     puts 'Invalid timestamp formatting'
irb(main):004:2>     timestamp
irb(main):005:2>   else
irb(main):006:2*     stamp
irb(main):007:2>   end
irb(main):008:1> end
=> :verify_timestamp
irb(main):009:0>
irb(main):010:0* def timestamp
irb(main):011:1>   print 'Enter timestamp for removal: '
irb(main):012:1>   ts = gets.chomp
irb(main):013:1>   verify_timestamp(ts)
irb(main):014:1> end
=> :timestamp
irb(main):015:0> verify_timestamp(timestamp)
Enter timestamp for removal: test
Invalid timestamp formatting
Enter timestamp for removal: 06/12/2016 8:18:54 pm
=> "06/12/2016 8:18:54 pm"
irb(main):016:0>

So there you have it, there are three different ways to verify your time stamps, depending on what you want to use them for might be another story, but any of these three should do the trick.

Share:
10,395
JohnDoeYo
Author by

JohnDoeYo

Updated on August 02, 2022

Comments

  • JohnDoeYo
    JohnDoeYo over 1 year

    I have a program that creates information for a time stamp, the time stamp has to match the following format: MM/DD/YYYY HH:MM:SS AM or PM

    For example:

    06/02/2016 10:14:56 AM

    09/14/2014 9:16:32 PM

    I need a way to match this information using preferable a regex, however it doesn't have to be a regex. Any help with this would be greatly appreciated.

    I know how to match the digits /\d+/, and non white space characters, however matching the digits won't grab the :, /, AM or PM. And grabbing the non white space will grab everything except for the obviously white space.

    So to recap I need a way to verify that a formatted time and date: MM/DD/YYYY HH:MM:SS AM or PM That can contain 20 to 22 characters (including white space) exists or not given as user input.

    Example of what I need:

    def timestamp
      print 'Enter the time stamp to verify: '
      ts = gets.chomp
      if !(ts[REGEX-HERE])
        puts 'Invalid formatting of time stamp'
        exit 1
      else
        puts ts
      end
    end
    
    # <= 6/1/2016 1:10:5 PM will output '6/1/2016 1:10:5 PM'
    # <= 06/01/2016 01:10:05 PM will output 'Invalid formatting of time stamp'
    # <= 10/1/2016 1:25:43 AM will output '10/1/2016 1:25:43 AM'
    # <= 10/01/2016 01:25:43 AM will output 'Invalid formatting of time stamp'