I would like to check the iso8601 format for the date entered in ruby , like start_date = "2011/05/05" should be matched for the format 2011-05-05T00:00:00-04:00 and errors returned accordingly. Should we use regex here or any method is present for this?
The date-time is inserted in the format YYYY-MM-DDThh:mm:ssTZD . Attribute Values: This attribute contains single value YYYY-MM-DDThh:mm:ssTZD which is used to specify the date and time when the text was deleted.
If you look at the original post you will see that the date was reported with a 'Z' at the end. That is the way it comes from Excel (At least until MS change the connector back). The UTC timecode will convert directly using formatdatetime.
Actually, the date is in ISO 8601 UTC time zone format. A formatDateTime () will work just fine without the extra steps. But be aware Excel using this format was due to a regression bug.
YYYY: It sets the year of datetime object (e.g. 2009). MM: It sets the month of datetime object (e.g. 05 for March). DD: It sets the day of the month of datetime object (e.g. 04). T: It is a required separator. hh: It sets the hour of datetime object (e.g. 18 for 06.00pm).
Sounds like you want Time.iso8601
:
require 'time'
iso = Time.iso8601(start_date)
See this blog post for more information.
EDIT: Here's a short but complete test program which works:
require 'time'
text = "2011-05-05T00:00:00-04:00"
parsed = Time.iso8601(text)
puts parsed
Output:
Thu May 05 04:00:00 UTC 2011
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