I couldn't find a good implementation of this on the internet, so I wrote one. Here's hoping this page becomes the first hit in Google. Enjoy!
/**
* Return a Javascript Date for the given XML Schema date string. Return
* null if the date cannot be parsed.
*
* Does not know how to parse BC dates or AD dates < 100.
*
* Valid examples of input:
* 2010-04-28T10:46:37.0123456789Z
* 2010-04-28T10:46:37.37Z
* 2010-04-28T10:46:37Z
* 2010-04-28T10:46:37
* 2010-04-28T10:46:37.012345+05:30
* 2010-04-28T10:46:37.37-05:30
* 1776-04-28T10:46:37+05:30
* 0150-04-28T10:46:37-05:30
*/
var xmlDateToJavascriptDate = function(xmlDate) {
// It's times like these you wish Javascript supported multiline regex specs
var re = /^([0-9]{4,})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(\.[0-9]+)?(Z|([+-])([0-9]{2}):([0-9]{2}))?$/;
var match = xmlDate.match(re);
if (!match)
return null;
var all = match[0];
var year = match[1]; var month = match[2]; var day = match[3];
var hour = match[4]; var minute = match[5]; var second = match[6];
var milli = match[7];
var z_or_offset = match[8]; var offset_sign = match[9];
var offset_hour = match[10]; var offset_minute = match[11];
if (offset_sign) { // ended with +xx:xx or -xx:xx as opposed to Z or nothing
var direction = (offset_sign == "+" ? 1 : -1);
hour = parseInt(hour) + parseInt(offset_hour) * direction;
minute = parseInt(minute) + parseInt(offset_minute) * direction;
}
var utcDate = Date.UTC(year, month, day, hour, minute, second, (milli || 0));
return new Date(utcDate);
}
Feel free to use, modify, and redistribute without credit.