]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
create moments with objects
authorIsaac Cambron <icambron@gmail.com>
Sat, 7 Sep 2013 22:44:29 +0000 (18:44 -0400)
committerIsaac Cambron <icambron@gmail.com>
Sat, 7 Sep 2013 22:44:29 +0000 (18:44 -0400)
moment.js
test/moment/create.js

index 03d66a4e02e82602d7467ffd5ca388f0e6ed97ea..39b08ea43903ed8623d6df7c10d3a02b63a516e9 100644 (file)
--- a/moment.js
+++ b/moment.js
         config._d = date;
     }
 
+    function dateFromObject(config) {
+        var o = config._i;
+
+        if (config._d) {
+            return;
+        }
+
+        config._a = [
+            o.years || o.year || o.y,
+            o.months || o.month || o.m,
+            o.days || o.day || o.d,
+            o.hours || o.hour || o.h,
+            o.minutes || o.minute || o.m,
+            o.seconds || o.second || o.s,
+            o.milliseconds || o.millisecond || o.ms
+        ];
+
+        dateFromArray(config);
+    }
+
     function currentDateArray(config) {
         var now = new Date();
         if (config._useUTC) {
         } else if (isArray(input)) {
             config._a = input.slice(0);
             dateFromArray(config);
+        } else if (input instanceof Date) {
+            config._d = new Date(+input);
+        } else if (typeof(input) == 'object') {
+            dateFromObject(config);
         } else {
-            config._d = input instanceof Date ? new Date(+input) : new Date(input);
+            config._d = new Date(input);
         }
     }
 
index 634b4514fc807d9f2ad77ea917294954c5995622..61928264d2b8ed8bfaf7d70876276760fc0b2489 100644 (file)
@@ -22,6 +22,19 @@ exports.create = {
         test.done();
     },
 
+    "object" : function (test) {
+        test.expect(8);
+        test.ok(moment({year: 2010}).toDate() instanceof Date, "{year: 2010}");
+        test.ok(moment({year: 2010, month: 1}).toDate() instanceof Date, "{year: 2010, month: 1}");
+        test.ok(moment({year: 2010, month: 1, day: 12}).toDate() instanceof Date, "{year: 2010, month: 1, day: 12}");
+        test.ok(moment({year: 2010, month: 1, day: 12, hours: 1}).toDate() instanceof Date, "{year: 2010, month: 1, day: 12, hours: 1}");
+        test.ok(moment({year: 2010, month: 1, day: 12, hours: 1, minutes: 1}).toDate() instanceof Date, "{year: 2010, month: 1, hours: 12, minutes: 1, seconds: 1}");
+        test.ok(moment({year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1}).toDate() instanceof Date, "{year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1]");
+        test.ok(moment({year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1, milliseconds: 1}).toDate() instanceof Date, "{year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1, milliseconds: 1]");
+        test.equal(+moment(new Date(2010, 1, 14, 15, 25, 50, 125)), +moment({year: 2010, month: 1, day: 14, hours: 15, minutes: 25, seconds: 50, milliseconds: 125}), "constructing with object === constructing with new Date()");
+        test.done();
+    },
+
     "multi format array copying": function (test) {
         var importantArray = ['MM/DD/YYYY', 'YYYY-MM-DD', 'MM-DD-YYYY'];
         test.expect(1);