Skip to content

Commit

Permalink
Merge pull request #2002 from ichernev:accept-date-key-object
Browse files Browse the repository at this point in the history
Accept 'date' when creating moment with object
  • Loading branch information
ichernev committed Nov 17, 2014
2 parents 9d604b9 + 639a224 commit 342389a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion moment.js
Expand Up @@ -1429,7 +1429,7 @@
config._a = [
normalizedInput.year,
normalizedInput.month,
normalizedInput.day,
normalizedInput.day || normalizedInput.date,
normalizedInput.hour,
normalizedInput.minute,
normalizedInput.second,
Expand Down
30 changes: 19 additions & 11 deletions test/moment/create.js
Expand Up @@ -43,17 +43,25 @@ exports.create = {
},

'object' : function (test) {
test.expect(10);
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({years: 2010, months: 1, days: 14, hours: 15, minutes: 25, seconds: 50, milliseconds: 125}), 'constructing with object (long plural) === constructing with new Date()');
test.equal(+moment(new Date(2010, 1, 14, 15, 25, 50, 125)), +moment({year: 2010, month: 1, day: 14, hour: 15, minute: 25, second: 50, millisecond: 125}), 'constructing with object (long) === constructing with new Date()');
test.equal(+moment(new Date(2010, 1, 14, 15, 25, 50, 125)), +moment({y: 2010, M: 1, d: 14, h: 15, m: 25, s: 50, ms: 125}), 'constructing with object (short) === constructing with new Date()');
var fmt = 'YYYY-MM-DD HH:mm:ss.SSS',
tests = [
[{year: 2010}, '2010-01-01 00:00:00.000'],
[{year: 2010, month: 1}, '2010-02-01 00:00:00.000'],
[{year: 2010, month: 1, day: 12}, '2010-02-12 00:00:00.000'],
[{year: 2010, month: 1, date: 12}, '2010-02-12 00:00:00.000'],
[{year: 2010, month: 1, day: 12, hours: 1}, '2010-02-12 01:00:00.000'],
[{year: 2010, month: 1, date: 12, hours: 1}, '2010-02-12 01:00:00.000'],
[{year: 2010, month: 1, day: 12, hours: 1, minutes: 1}, '2010-02-12 01:01:00.000'],
[{year: 2010, month: 1, date: 12, hours: 1, minutes: 1}, '2010-02-12 01:01:00.000'],
[{year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1}, '2010-02-12 01:01:01.000'],
[{year: 2010, month: 1, day: 12, hours: 1, minutes: 1, seconds: 1, milliseconds: 1}, '2010-02-12 01:01:01.001'],
[{years: 2010, months: 1, days: 14, hours: 15, minutes: 25, seconds: 50, milliseconds: 125}, '2010-02-14 15:25:50.125'],
[{year: 2010, month: 1, day: 14, hour: 15, minute: 25, second: 50, millisecond: 125}, '2010-02-14 15:25:50.125'],
[{y: 2010, M: 1, d: 14, h: 15, m: 25, s: 50, ms: 125}, '2010-02-14 15:25:50.125']
], i;
for (i = 0; i < tests.length; ++i) {
test.equal(moment(tests[i][0]).format(fmt), tests[i][1]);
}
test.done();
},

Expand Down

0 comments on commit 342389a

Please sign in to comment.