how can i validate that the date and time in the following string is in the right format i.e year, month, day and then the time(4 digits, 2 digits, 2 digits and then the time)
"Event (No 3) 0007141706 at 2010/04/27 11:48 ( Pacific )"
thanks
Why create your own regular expressions when Ruby can handle the parsing for you?
>> require 'date'
=> true
>> str = "Event (No 3) 0007141706 at 2010/04/27 11:48 ( Pacific )"
>> dt = DateTime.parse(str)
=> #<DateTime: 2010-04-27T11:48:00-08:00 (98212573/40,-1/3,2299161)>
This also makes sure the date is valid, not just in a recognizable format:
>> str = "Event (No 3) 0007141706 at 2010/13/32 25:61 ( Pacific )"
>> dt = DateTime.parse(str)
ArgumentError: invalid date
/Event \(No \d+\) \d+ at (\d{4})\/(\d{2})\/(\d{2}) (\d\d):(\d\d) \([\w\s]+\)/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With