ISO Date and Time for Programmers
Monday 28th August 2006, 10:37Markus Kuhn has the best writeup on ISO 8601 (date and time formatting) that I’ve seen. Concise and to the point it’s just what you need to read to sort things out. Check it out -> International standard date and time notation.
For those of you who like regular expressions (regexes for short) here is my take on a regex to parse a ISO-date into it’s components. It supports date-only, date & time, date & time +timezone and both short and long formats may be used (it’s possible to exclude the separators.
The expression below should be used as a single row. The first row matches the date-part, the second and third which are optional, maches the time and then there is the also optional, timezone pattern.
^(([0-9]{4})-?([0-1][0-9])-?([0-3][0-9]))
[ T]?(([0-2][0-9]):?([0-5][0-9])
(:?([0-5][0-9])([,.][0-9]+)?)?)?
((Z)|(([+-][0-2][0-9]):?([0-9]{2})?))?
Please not that this should not be used for ISO-8601 validation since it doesn’t support the week or day of year notations. It’s also not strict regarding “valid” months and days of month. It’s possible to enter month 18 for example. I choose to leave that out since I only needed to parse expressions and didn’t want the regex to be longer than necessary.