]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Adding moment.fn.toJSON
authorTim Wood <washwithcare@gmail.com>
Mon, 22 Oct 2012 21:17:22 +0000 (14:17 -0700)
committerTim Wood <washwithcare@gmail.com>
Mon, 22 Oct 2012 21:17:22 +0000 (14:17 -0700)
#444

moment.js
test/moment/format.js

index 3e6e241a4933fcfc7a5ef3c8314865cefda52e90..2c36cea4e6bf304e10cbbe4b73e63194178cc59f 100644 (file)
--- a/moment.js
+++ b/moment.js
             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 [
index a4147a67bb49bace583542ea4e472d5bbaca2795..88c2fe0b344b97eba8b182496c1e54848f4b3c3c 100644 (file)
@@ -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();
     }
 };