Skip to content

Commit

Permalink
Adding moment.fn.toJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
timrwood committed Oct 22, 2012
1 parent e6d25ed commit 136636e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
13 changes: 13 additions & 0 deletions test/moment/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

0 comments on commit 136636e

Please sign in to comment.