Merge pull request #48 from alexandrnikitin/Issue47_UtcConversionDoesntWorkCorrectly

(Fixes #47) Fix conversion of date to UTC
This commit is contained in:
Sami Samhuri 2015-03-04 13:43:55 -08:00
commit 9146f45142
2 changed files with 19 additions and 4 deletions

View file

@ -279,10 +279,24 @@
});
}
function dateToUTC(d) {
var msDelta = (d.getTimezoneOffset() || 0) * 60000;
return new Date(d.getTime() + msDelta);
}
function dateToUTC(d) {
var year = d.getUTCFullYear();
var date = new Date(
year,
d.getUTCMonth(),
d.getUTCDate(),
d.getUTCHours(),
d.getUTCMinutes(),
d.getUTCSeconds(),
d.getUTCMilliseconds()
);
// In old dates, years is incorrectly interpreted as a 2-digit year with base 1900.
// Correct this by setting the year explicitly after the fuzzy creation process.
if (date.getFullYear() != year) {
date.setFullYear(year);
}
return date;
}
var RequiredDateMethods = ['getTime', 'getTimezoneOffset', 'getDay', 'getDate', 'getMonth', 'getFullYear', 'getYear', 'getHours', 'getMinutes', 'getSeconds'];
function quacksLikeDate(x) {

View file

@ -104,6 +104,7 @@ assert.format('%y', '11')
assert.format('%Z', null, 'GMT')
assert.format('%z', null, '+0000')
assert.format('%%', '%') // any other char
assert.format('%F %T', null, '1970-01-01 00:00:00', new Date(0))
ok('GMT')