When I tried in javascript to calculate the ISO 8601 Date String, I noticed that there is already such method on the
This method is called toISOString and is not yet documented at the mdc page.
This date format is needed for instance for the amazon web services.
If you need a implementation anyways (even though toISOString should be available), here is a plain javascript implementation:
function toISO8601(date) {
var pad_two = function(n) {
return (n < 10 ? '0' : '') + n;
};
var pad_three = function(n) {
return (n < 100 ? '0' : '') + (n < 10 ? '0' : '') + n;
};
return [
date.getUTCFullYear(),
'-',
pad_two(date.getUTCMonth() + 1),
'-',
pad_two(date.getUTCDate()),
'T',
pad_two(date.getUTCHours()),
':',
pad_two(date.getUTCMinutes()),
':',
pad_two(date.getUTCSeconds()),
'.',
pad_three(date.getUTCMilliseconds()),
'Z',
].join('');
}
It takes a javascript date object and converts it to an iso 8601 date string.