]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Added tests and behavior to accept JSON-encoded date values.
authorOliver, Jonathan <joliver@localhost>
Thu, 29 Dec 2011 05:08:09 +0000 (22:08 -0700)
committerOliver, Jonathan <joliver@localhost>
Thu, 29 Dec 2011 05:08:09 +0000 (22:08 -0700)
For more information, see http://blog.activa.be/index.php/2010/03/handling-dates-in-json-responses-with-jquery-1-4-the-easy-way/

moment.js
sitesrc/js/unit-tests.js

index 4167f7270983313e0b05e6b9ba4f87fe848d80e6..fa322a71ea73fe08fe84a80773e5c923c0548368 100644 (file)
--- a/moment.js
+++ b/moment.js
@@ -13,6 +13,7 @@
         hasModule = (typeof module !== 'undefined'),
         paramsToParse = 'months|monthsShort|monthsParse|weekdays|weekdaysShort|longDateFormat|calendar|relativeTime|ordinal|meridiem'.split('|'),
         i,
+        jsonRegex = /\/?Date\((\d+)((?:\-|\+)\d+)?\)\/?/i,
         charactersToReplace = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?|ZZ?|LT|LL?L?L?)/g,
         nonuppercaseLetters = /[^A-Z]/g,
         timezoneRegex = /\([A-Za-z ]+\)|:[0-9]{2} [A-Z]{3} /g,
         if (input === null) {
             return null;
         }
-        var date;
+        var date, matched;
         // parse UnderscoreDate object
         if (input && input._d instanceof Date) {
             date = new Date(+input._d);
             } else {
                 date = makeDateFromStringAndFormat(input, format);
             }
+        // evaluate it as a JSON-encoded date
+        } else if (matched = jsonRegex.exec(input)) {
+            date = new Date(parseInt(matched[1]));
         // parse everything else
         } else {
             date = input === undefined ? new Date() :
index 8f76abacb56c53db4a2551575a541c6a03c12a26..43c91c35759bbcf31f6e0fb42b4ddbd171f21b5b 100755 (executable)
@@ -57,6 +57,12 @@ test("string without format", 2, function() {
     ok(moment("Mon, 25 Dec 1995 13:30:00 GMT").native() instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT");
 });
 
+test("string without format - json", 4, function() {
+    ok(moment("Date(1325132654000)").valueOf() === 1325132654000, "Date(1325132654000)");
+    ok(moment("/Date(1325132654000)/").valueOf() === 1325132654000, "/Date(1325132654000)/");
+    ok(moment("/Date(1325132654000+0700)/").valueOf() === 1325132654000, "/Date(1325132654000+0700)/");
+    ok(moment("/Date(1325132654000-0700)/").valueOf() === 1325132654000, "/Date(1325132654000-0700)/");
+});
 
 test("string with format", 18, function() {
     moment.lang('en');