From 5b7f261716bc5318b8c03ee0490971ad10f116e2 Mon Sep 17 00:00:00 2001 From: Iskren Chernev Date: Sun, 24 Feb 2013 06:10:30 -0300 Subject: [PATCH] Moment creation defaults to current date --- moment.js | 48 +++++++++++++++++++++++++++++++++++-------- test/moment/create.js | 26 ++++++++++++++++++++--- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/moment.js b/moment.js index f02c66109..048153509 100644 --- a/moment.js +++ b/moment.js @@ -316,8 +316,7 @@ days = duration._days, months = duration._months, minutes, - hours, - currentDate; + hours; if (milliseconds) { mom._d.setTime(+mom._d + milliseconds * isAdding); @@ -702,7 +701,9 @@ // 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' : @@ -715,11 +716,17 @@ } 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; @@ -782,13 +789,24 @@ // 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]; } @@ -809,6 +827,19 @@ 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` @@ -1300,8 +1331,7 @@ month : function (input) { var utc = this._isUTC ? 'UTC' : '', - dayOfMonth, - daysInMonth; + dayOfMonth; if (input != null) { if (typeof input === 'string') { diff --git a/test/moment/create.js b/test/moment/create.js index f5dda6319..b7c3249d0 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -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); -- 2.47.2