Posts
read more
format javascript date in ISO 8601
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, to solve this problem, we can provide our own prototype function for Date if it doesn’t exist: