format javascript date in ISO 8601
By Anatoly Mironov
There is a solution in StackOverflow:
/\* use a function for the exact format desired... \*/
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'}
var d = new Date();
print(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z
```But there is already a function for this: toISOString, it came with ecmascript 5. [Unfortunately, not all browsers support this](http://kangax.github.com/es5-compat-table/ "See the best comparision for ecmascript 5 support"), to solve this problem, we can provide our own prototype function for Date if it doesn't exist:
if (!Date.prototype.toISOString) { Date.prototype.toISOString = function() { function pad(n) { return n < 10 ? ‘0’ + n : n } return this.getUTCFullYear() + ‘-’ + pad(this.getUTCMonth() + 1) + ‘-’ + pad(this.getUTCDate()) + ‘T’ + pad(this.getUTCHours()) + ‘:’ + pad(this.getUTCMinutes()) + ‘:’ + pad(this.getUTCSeconds()) + ‘Z’; }; }
var today = DateTime.Today; var monthAgo = today.AddDays(-30); var t = monthAgo.ToString(“s”);
Sys.Serialization.JavaScriptSerializer.deserialize
#### Parse Dates From ISO 8601
What if you want to create a date from iso in javascript. It is no problem if you use modern browsers: var date = new Date(isoString); But not in IE. Here it fails. So we have to create custom parsing functions for that. D-oh. (One solution could be to use moment.js):
Date.fromISOString = function (iso) { ///Is used to parse iso 8601 dates in IE ///partly inspired from https://github.com/csnover/js-iso8601/blob/master/iso8601.js ///and http://n8v.enteuxis.org/2010/12/parsing-iso-8601-dates-in-javascript/ /// var date = new Date(iso); if (isNaN(date)) { var minutesOffset = 0, numericKeys = [1, 4, 5, 6, 7, 10, 11]; var struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/ .exec(iso); // avoid NaN timestamps caused by “undefined” values being passed to Date.UTC for (var i = 0, k; (k = numericKeys[i]); ++i) { struct[k] = +struct[k] || 0; }
// allow undefined days and months
struct\[2\] = (+struct\[2\] || 1) - 1;
struct\[3\] = +struct\[3\] || 1;
if (struct\[8\] !== 'Z' && struct\[9\] !== undefined) {
minutesOffset = struct\[10\] \* 60 + struct\[11\];
if (struct\[9\] === '+') {
minutesOffset = 0 - minutesOffset;
}
}
var s = Date.UTC(struct\[1\], struct\[2\], struct\[3\], struct\[4\], struct\[5\] + minutesOffset, struct\[6\], struct\[7\]);
date = new Date(s);
}
return date;
};
## Comments from Wordpress.com
####
[Anatoly Mironov]( "mirontoli@gmail.com") - <time datetime="2012-12-28 09:36:13">Dec 5, 2012</time>
I'm glad it helped!
<hr />
####
[joarobles](http://gravatar.com/joarobles "joaquinlrobles@gmail.com") - <time datetime="2012-12-27 14:45:22">Dec 4, 2012</time>
excellent! i was looking for this functionality for the Date() object and finally used the ISODateString() you provide
<hr />