Вот пример:
Пример вывода.Обратите внимание, как DurationGMT
& DurationLocal
отличаются.При сравнении с датами всегда используйте GMT.
Now: 1,326,054,979,124 ms (Sun, 08 Jan 2012 20:36:19 GMT)
Start1: 1,325,376,000,000 ms (Sun, 01 Jan 2012 00:00:00 GMT)
Start2: 1,325,376,000,000 ms (Sun, 01 Jan 2012 00:00:00 GMT)
Start3: 1,325,376,000,000 ms (Sun, 01 Jan 2012 00:00:00 GMT)
DurationGMT: 678,979,124 ms (Accurate method)
StartLocal1: 1,325,397,600,000 ms (Sun, 01 Jan 2012 06:00:00 GMT)
DurationLocal: 657,379,124 ms !!! Don't use this method
Вот три способа получить дату по Гринвичу, # 3 будет тем, что вы хотите.
var now = new Date();
var startOfYear1 = createGMTDate1(2012, 0, 1, 0, 0, 0, 0);
var startOfYear2 = createGMTDate2(2012, 0, 1, 0, 0, 0, 0);
var startOfYear3 = createGMTDate3(2012, 0, 1, 0, 0, 0, 0);
var durationGMTMillis = now.getTime() - startOfYear1.getTime(); // accurate
var startOfYearLocal1 = new Date(2012, 0, 1, 0, 0, 0, 0);
var durationLocalMillis = now.getTime() - startOfYearLocal1.getTime(); // inaccurate
function createGMTDate1(year, month, date, hours, mins, secs, millis) {
var dateDefaultTz = new Date(year, month, date, hours, mins, secs, millis);
var localeTzGMTMillis = dateDefaultTz.getTime();
var localeTzGMTOffsetMillis = dateDefaultTz.getTimezoneOffset() * 60 * 1000;
var dateGMT = new Date(localeTzGMTMillis - localeTzGMTOffsetMillis);
return dateGMT;
}
function createGMTDate2(year, month, date, hours, mins, secs, millis) {
var dateGMT = new Date(0);
dateGMT.setUTCFullYear(year);
dateGMT.setUTCMonth(month);
dateGMT.setUTCDate(date);
dateGMT.setUTCHours(hours);
dateGMT.setUTCMinutes(mins);
dateGMT.setUTCSeconds(secs);
dateGMT.setUTCMilliseconds(millis);
return dateGMT;
}
function createGMTDate3(year, month, date, hours, mins, secs, millis) {
var dateGMT = new Date(Date.UTC(year, month, date, hours, mins, secs, millis));
return dateGMT;
}