var d = Date.parseDate("2005-10-05 12:13 am", "Y-m-d g:i a"); document.write(d + "\n"); var d = Date.parseDate("9/5/05", "n/j/y"); document.write(d + "\n");
generates this output:
It is possible to use the date-parsing functionality, together with the
date-formatting functionality, for round-trip formatting and parsing back again
without losing information. All you need to do is use the same format
specifier for both operations. In general, I would suggest using one of the
ISO 8601 patterns, such as
ensure no precision is lost along the way. Obviously you need to use a format
specifier that doesn't discard information during the formatting step; anything
discarded is unrecoverable during the parsing step.
To demonstrate this functionality, here is a table showing the round-trip for all the predefined format patterns.
As you can see, some of the formats discard some information, so it's not always possible to reconstitute the original input. Notice particularly how the date is assumed to be "today" in cases where only the time is preserved in the formatting string.
Finally, a word about the internals. As you may have seen me do before, I've written code that writes code. Here is the regular expression generated for one of the parsing formats, followed by the automatically generated code that uses the regular expression to implement the parser: