]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Moment creation defaults to current date 912/head
authorIskren Chernev <iskren.chernev@gmail.com>
Sun, 24 Feb 2013 09:10:30 +0000 (06:10 -0300)
committerIskren Chernev <iskren.chernev@gmail.com>
Sat, 20 Jul 2013 16:54:52 +0000 (14:54 -0200)
moment.js
test/moment/create.js

index f02c661097efd56f04e9be61fd156f83f72b99f7..048153509a9e7b1367b262fc47c75098b6acd332 100644 (file)
--- a/moment.js
+++ b/moment.js
             days = duration._days,
             months = duration._months,
             minutes,
-            hours,
-            currentDate;
+            hours;
 
         if (milliseconds) {
             mom._d.setTime(+mom._d + milliseconds * isAdding);
         // MONTH
         case 'M' : // fall through to MM
         case 'MM' :
-            datePartArray[1] = (input == null) ? 0 : ~~input - 1;
+            if (input != null) {
+                datePartArray[1] = ~~input - 1;
+            }
             break;
         case 'MMM' : // fall through to MMMM
         case 'MMMM' :
             }
             break;
         // DAY OF MONTH
-        case 'D' : // fall through to DDDD
-        case 'DD' : // fall through to DDDD
+        case 'D' : // fall through to DD
+        case 'DD' :
+            if (input != null) {
+                datePartArray[2] = ~~input;
+            }
+            break;
+        // DAY OF YEAR
         case 'DDD' : // fall through to DDDD
         case 'DDDD' :
             if (input != null) {
+                datePartArray[1] = 0;
                 datePartArray[2] = ~~input;
             }
             break;
     // note: all values past the year are optional and will default to the lowest possible value.
     // [year, month, day , hour, minute, second, millisecond]
     function dateFromArray(config) {
-        var i, date, input = [];
+        var i, date, input = [], currentDate;
 
         if (config._d) {
             return;
         }
 
-        for (i = 0; i < 7; i++) {
+        // Default to current date.
+        // * if no year, month, day of month are given, default to today
+        // * if day of month is given, default month and year
+        // * if month is given, default only year
+        // * if year is given, don't default anything
+        currentDate = currentDateArray(config);
+        for (i = 0; i < 3 && config._a[i] == null; ++i) {
+            config._a[i] = input[i] = currentDate[i];
+        }
+
+        // Zero out whatever was not defaulted, including time
+        for (; i < 7; i++) {
             config._a[i] = input[i] = (config._a[i] == null) ? (i === 2 ? 1 : 0) : config._a[i];
         }
 
         config._d = date;
     }
 
+    function currentDateArray(config) {
+        var now = new Date();
+        if (config._useUTC) {
+            return [
+                now.getUTCFullYear(),
+                now.getUTCMonth(),
+                now.getUTCDate()
+            ];
+        } else {
+            return [now.getFullYear(), now.getMonth(), now.getDate()];
+        }
+    }
+
     // date from string and format string
     function makeDateFromStringAndFormat(config) {
         // This array is used to make a Date, either with `new Date` or `Date.UTC`
 
         month : function (input) {
             var utc = this._isUTC ? 'UTC' : '',
-                dayOfMonth,
-                daysInMonth;
+                dayOfMonth;
 
             if (input != null) {
                 if (typeof input === 'string') {
index f5dda63193dc1a2fe38e1b15a7925bac9f1595cb..b7c3249d00fc0d9d8cb5ca25a2d41cab9f2850e7 100644 (file)
@@ -115,13 +115,33 @@ exports.create = {
     "empty string with formats" : function (test) {
         test.expect(3);
 
-        test.equal(moment(' ', 'MM').format('YYYY-MM-DD HH:mm:ss'), '0000-01-01 00:00:00', 'should not break if input is an empty string');
-        test.equal(moment(' ', 'DD').format('YYYY-MM-DD HH:mm:ss'), '0000-01-01 00:00:00', 'should not break if input is an empty string');
-        test.equal(moment(' ', ['MM', "DD"]).format('YYYY-MM-DD HH:mm:ss'), '0000-01-01 00:00:00', 'should not break if input is an empty string');
+        var currentDate = moment().startOf('day').format('YYYY-MM-DD HH:mm:ss');
+        test.equal(moment(' ', 'MM').format('YYYY-MM-DD HH:mm:ss'), currentDate, 'should not break if input is an empty string');
+        test.equal(moment(' ', 'DD').format('YYYY-MM-DD HH:mm:ss'), currentDate, 'should not break if input is an empty string');
+        test.equal(moment(' ', ['MM', "DD"]).format('YYYY-MM-DD HH:mm:ss'), currentDate, 'should not break if input is an empty string');
 
         test.done();
     },
 
+    "defaulting to current date" : function (test) {
+        test.expect(4);
+
+        var now = moment();
+        test.equal(moment('12:13:14', 'hh:mm:ss').format('YYYY-MM-DD hh:mm:ss'),
+                now.clone().hour(12).minute(13).second(14).format('YYYY-MM-DD hh:mm:ss'),
+                'given only time default to current date');
+        test.equal(moment('05', 'DD').format('YYYY-MM-DD'),
+                now.clone().date(5).format('YYYY-MM-DD'),
+                'given day of month default to current month, year');
+        test.equal(moment('05', 'MM').format('YYYY-MM-DD'),
+                now.clone().month(4).date(1).format('YYYY-MM-DD'),
+                'given month default to current year');
+        test.equal(moment('1996', 'YYYY').format('YYYY-MM-DD'),
+                now.clone().year(1996).month(0).date(1).format('YYYY-MM-DD'),
+                'given year do not default');
+        test.done();
+    },
+
     "matching am/pm" : function (test) {
         test.expect(13);