From: Tim Wood Date: Mon, 22 Oct 2012 21:17:22 +0000 (-0700) Subject: Adding moment.fn.toJSON X-Git-Tag: 2.0.0~62 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=136636eae95021c20c14e18ec158d080063eef96;p=thirdparty%2Fmoment.git Adding moment.fn.toJSON #444 --- diff --git a/moment.js b/moment.js index 3e6e241a4..2c36cea4e 100644 --- a/moment.js +++ b/moment.js @@ -997,6 +997,10 @@ return this._d; }, + toJSON : function () { + return moment.utc(this).format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]'); + }, + toArray : function () { var m = this; return [ diff --git a/test/moment/format.js b/test/moment/format.js index a4147a67b..88c2fe0b3 100644 --- a/test/moment/format.js +++ b/test/moment/format.js @@ -124,5 +124,18 @@ exports.format = { test.equal(date.format("MMM 'YY"), "Jan '12", "Should be able to format with single parenthesis"); test.equal(date.format("MMM \"YY"), 'Jan "12', "Should be able to format with double parenthesis"); test.done(); + }, + + "toJSON" : function(test) { + var supportsJson = typeof JSON !== "undefined" && JSON.stringify && JSON.stringify.call, + date = moment.utc("2012-10-09T20:30:40.678"); + + test.expect(supportsJson ? 2 : 1); + + test.equal(date.toJSON(), "2012-10-09T20:30:40.678Z", "should output ISO8601 on moment.fn.toJSON"); + test.equal(JSON.stringify({ + date : date + }), '{"date":"2012-10-09T20:30:40.678Z"}', "should output ISO8601 on JSON.stringify"); + test.done(); } };