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') {
"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);