Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended years Date.prototype.toJSON #25

Closed
petermichaux opened this issue Jan 29, 2012 · 1 comment
Closed

Extended years Date.prototype.toJSON #25

petermichaux opened this issue Jan 29, 2012 · 1 comment

Comments

@petermichaux
Copy link

The Date.prototype.toJSON polyfill included in json2.js does not appear to allow for the correct required ability of representing extended years as described in 15.9.1.15.1 of the ECMAScript 5.1 specification and ISO 8601 4.1.3.3. The ISO document shows that years must be padded with zeros in the extended format.

function f(n) {
    // Format integers to have at least two digits.
    return n < 10 ? '0' + n : n;
}

Date.prototype.toJSON = function (key) {

    return isFinite(this.valueOf())
        ? this.getUTCFullYear() + '-' +
            f(this.getUTCMonth() + 1) + '-' +
            f(this.getUTCDate()) + 'T' +
            f(this.getUTCHours()) + ':' +
            f(this.getUTCMinutes()) + ':' +
            f(this.getUTCSeconds()) + 'Z'
        : null;
};

(new Date(-62198755200000)).toJSON()

The above produces "-1-01-01T00:00:00Z" but should produce "-000001-01-01T00:00:00Z"

There is an example of padding correctly in the es5-shim

https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js#L860

@douglascrockford
Copy link
Owner

Please let me know if this ever manifests as a real problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants