Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix parsing/formatting of first century dates. #366

Closed
wants to merge 1 commit into from

2 participants

@jasondavies

According to ECMA-262, new Date(y, …) will auto-convert y to 1900 + y if 0 ≤ y ≤ 99, hence setFullYear or setUTCFullYear is used instead.

This also zero-pads the year for the "YYYY" format, since the year may not necessarily have four digits.

@jasondavies jasondavies Fix parsing/formatting of first century dates.
According to ECMA-262, `new Date(y, …)` will auto-convert `y` to `1900 +
y` if `0 ≤ y ≤ 99`, hence setFullYear or setUTCFullYear is used instead.

This also zero-pads the year for the "YYYY" format, since the year may
not necessarily have four digits.
71b4e1f
@timrwood
Owner

Wow, this looks nice.

I think this should be safe to merge in. I hope no-one was relying on moment([0]) being Jan 1 1900...

In theory, moment([-100, 0, 1]) should work as well now, no?

Also, you should work off the develop branch as that has the latest code, and some of the code you wrote will need to be re-written. For example, the internal function dateFromArray now takes an asUTC parameter so the rest of the codebase isn't littered with config.isUTC ? new Date(Date.UTC.apply({}, datePartArray)) : dateFromArray(datePartArray);. Instead, it's return dateFromArray(datePartArray, config.isUTC);

https://github.com/timrwood/moment/blob/c018776284d0bcd9b3a4e67160336ff7b85915ae/moment.js#L270

Also, there was some stuff added so that moment.fn.isValid would work on parsed strings and array input.

I'm going to close this PR, but please create a new one based on the develop branch and submit the PR to the develop branch.

Thanks for contributing!

@timrwood timrwood closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 17, 2012
  1. @jasondavies

    Fix parsing/formatting of first century dates.

    jasondavies authored
    According to ECMA-262, `new Date(y, …)` will auto-convert `y` to `1900 +
    y` if `0 ≤ y ≤ 99`, hence setFullYear or setUTCFullYear is used instead.
    
    This also zero-pads the year for the "YYYY" format, since the year may
    not necessarily have four digits.
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 6 deletions.
  1. +17 −3 moment.js
  2. +3 −3 test/moment/create.js
View
20 moment.js
@@ -172,7 +172,21 @@
// 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(input) {
- return new Date(input[0], input[1] || 0, input[2] || 1, input[3] || 0, input[4] || 0, input[5] || 0, input[6] || 0);
+ var date = new Date(0);
+ date.setFullYear(input[0], input[1] || 0, input[2] || 1);
+ date.setHours(input[3] || 0, input[4] || 0, input[5] || 0, input[6] || 0);
+ return date;
+ }
+
+ // convert an array to a UTC date.
+ // the array should mirror the parameters below
+ // note: all values past the year are optional and will default to the lowest possible value.
+ // [year, month, day , hour, minute, second, millisecond]
+ function dateFromArrayUTC(input) {
+ var date = new Date(0);
+ date.setUTCFullYear(input[0], input[1] || 0, input[2] || 1);
+ date.setUTCHours(input[3] || 0, input[4] || 0, input[5] || 0, input[6] || 0);
+ return date;
}
// format date using native date object
@@ -249,7 +263,7 @@
case 'YY' :
return leftZeroFill(currentYear % 100, 2);
case 'YYYY' :
- return currentYear;
+ return leftZeroFill(currentYear, 4);
// AM / PM
case 'a' :
return meridiem ? meridiem(currentHours, currentMinutes, false) : (currentHours > 11 ? 'pm' : 'am');
@@ -456,7 +470,7 @@
datePartArray[3] += config.tzh;
datePartArray[4] += config.tzm;
// return
- return config.isUTC ? new Date(Date.UTC.apply({}, datePartArray)) : dateFromArray(datePartArray);
+ return (config.isUTC ? dateFromArrayUTC : dateFromArray)(datePartArray);
}
// compare two arrays, return the number of differences
View
6 test/moment/create.js
@@ -78,9 +78,9 @@ exports.create = {
"empty string with formats" : function(test) {
test.expect(3);
- test.equal(moment(' ', 'MM').format('YYYY-MM-DD HH:mm:ss'), '1900-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'), '1900-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'), '1900-01-01 00:00:00', 'should not break if input is an empty string');
+ 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');
test.done();
},
Something went wrong with that request. Please try again.