Like many, I've long been frustrated with the quirky DateTime serialisation employed by the
Using this converter meant that a DateTime that previously would have been serialised as
"\/Date(1293840000000)\/" would now be serialised as
"2011-01-01T00:00:00.0000000Z" instead. This is entirely agreeable because 1. it's entirely clear what a
"2011-01-01T00:00:00.0000000Z" style date represents and
2. this is more in line with native browser JSON implementations and
<statingTheObvious>consistency is a good thing.
Sebastian alluded in his post to a
web.config setting that could be used to get web services / pagemethods etc. implementing his custom DateTime serialiser. This is it:
With this in place your web services / page methods will happily be able to serialise / deserialise ISO style date strings to your hearts content. ## What no ISO 8601 date string Date constructor?
With this in place I could parse ISO 8601 Dates just like anyone else. Great stuff.
Sat Jan 1 00:00:00 UTC 2011. Obviously in the fullness of time the parseISO8601Date solution should no longer be necessary because EcmaScript 5 specifies an ISO 8601 date string constructor. However, in the interim Nathan's solution is a lifesaver. Thanks again both to Sebastian Markbåge and Nathan Vonnahme who have both generously allowed me use their work as the basis for this post. ## PS And it would have worked if it wasn't for that pesky IE 9...
new Date("2011-01-01T00:00:00.0000000Z"). It hasn't. Take a look:
Sebastian got in contact after I first published this post and generously pointed out that, contrary to my expectation, IE 9 technically had the correct implementation. According to the EMCAScript standard the Date constructor should not allow more than millisecond precision. In this case, Chrome and Firefox are being less strict - not more correct. On reflection this does rather make sense as the result of a
JSON.stringify(new Date()) never results in an ISO date string to the 10 millionths of a second detail. Sebastian has himself stopped using Microsoft's Round-trip ("O", "o") Format Specifier in favour of this format string: ```cs
return new CustomString(((DateTime)obj).ToUniversalTime()