dracoblue.net

ISO 8601 Date String with Node.JS for AmazonWS

When I tried in javascript to calculate the ISO 8601 Date String, I noticed that there is already such method on the

Date object.

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.

In javascript, node.js by @ 03 Oct 2010

comments powered by Disqus

Recent Files

Advertisement

Recent Dev-Articles

Read recently

About

Blogroll