这是我的一点JS代码,这是需要的:

var secDiff = Math.abs(Math.round((utc_date-this.premiere_date)/1000));
this.years = this.calculateUnit(secDiff,(86400*365));
this.days = this.calculateUnit(secDiff-(this.years*(86400*365)),86400);
this.hours = this.calculateUnit((secDiff-(this.years*(86400*365))-(this.days*86400)),3600);
this.minutes = this.calculateUnit((secDiff-(this.years*(86400*365))-(this.days*86400)-(this.hours*3600)),60);
this.seconds = this.calculateUnit((secDiff-(this.years*(86400*365))-(this.days*86400)-(this.hours*3600)-(this.minutes*60)),1);

我想在“前”得到日期时间,但如果DST正在使用,那么日期是1小时。我不知道如何检查夏令时是否有效。

我怎样才能知道夏令时何时开始和结束?


当前回答

js库在它的time对象上提供了一个. isdst()方法。

moment#isDST检查当前时刻是否属于夏时制。

moment([2011, 2, 12]).isDST(); // false, March 12 2011 is not DST
moment([2011, 2, 14]).isDST(); // true, March 14 2011 is DST

其他回答

面向未来的解决方案,适用于所有时区

设x为在不考虑夏时制的情况下进入利息年的预期毫秒数。 设y为从感兴趣日期的年份开始到Epoch的毫秒数。 设z为自感兴趣的完整日期和时间的Epoch以来的毫秒数 设t是z减去x和y: z - y - x。这就得到了由于夏令时而产生的偏移量。 如果t为零,则DST不生效。如果t不为零,则DST生效。

"use strict"; function dstOffsetAtDate(dateInput) { var fullYear = dateInput.getFullYear()|0; // "Leap Years are any year that can be exactly divided by 4 (2012, 2016, etc) // except if it can be exactly divided by 100, then it isn't (2100,2200,etc) // except if it can be exactly divided by 400, then it is (2000, 2400)" // (https://www.mathsisfun.com/leap-years.html). var isLeapYear = ((fullYear & 3) | (fullYear/100 & 3)) === 0 ? 1 : 0; // (fullYear & 3) = (fullYear % 4), but faster //Alternative:var isLeapYear=(new Date(currentYear,1,29,12)).getDate()===29?1:0 var fullMonth = dateInput.getMonth()|0; return ( // 1. We know what the time since the Epoch really is (+dateInput) // same as the dateInput.getTime() method // 2. We know what the time since the Epoch at the start of the year is - (+new Date(fullYear, 0)) // day defaults to 1 if not explicitly zeroed // 3. Now, subtract what we would expect the time to be if daylight savings // did not exist. This yields the time-offset due to daylight savings. - (( (( // Calculate the day of the year in the Gregorian calendar // The code below works based upon the facts of signed right shifts // • (x) >> n: shifts n and fills in the n highest bits with 0s // • (-x) >> n: shifts n and fills in the n highest bits with 1s // (This assumes that x is a positive integer) -1 + // first day in the year is day 1 (31 & ((-fullMonth) >> 4)) + // January // (-11)>>4 = -1 ((28 + isLeapYear) & ((1-fullMonth) >> 4)) + // February (31 & ((2-fullMonth) >> 4)) + // March (30 & ((3-fullMonth) >> 4)) + // April (31 & ((4-fullMonth) >> 4)) + // May (30 & ((5-fullMonth) >> 4)) + // June (31 & ((6-fullMonth) >> 4)) + // July (31 & ((7-fullMonth) >> 4)) + // August (30 & ((8-fullMonth) >> 4)) + // September (31 & ((9-fullMonth) >> 4)) + // October (30 & ((10-fullMonth) >> 4)) + // November // There are no months past December: the year rolls into the next. // Thus, fullMonth is 0-based, so it will never be 12 in Javascript (dateInput.getDate()|0) // get day of the month )&0xffff) * 24 * 60 // 24 hours in a day, 60 minutes in an hour + (dateInput.getHours()&0xff) * 60 // 60 minutes in an hour + (dateInput.getMinutes()&0xff) )|0) * 60 * 1000 // 60 seconds in a minute * 1000 milliseconds in a second - (dateInput.getSeconds()&0xff) * 1000 // 1000 milliseconds in a second - dateInput.getMilliseconds() ); } // Demonstration: var date = new Date(2100, 0, 1) for (var i=0; i<12; i=i+1|0, date.setMonth(date.getMonth()+1|0)) console.log(date.getMonth()+":\t"+dstOffsetAtDate(date)/60/60/1000+"h\t"+date); date = new Date(1900, 0, 1); for (var i=0; i<12; i=i+1|0, date.setMonth(date.getMonth()+1|0)) console.log(date.getMonth()+":\t"+dstOffsetAtDate(date)/60/60/1000+"h\t"+date); // Performance Benchmark: console.time("Speed of processing 16384 dates"); for (var i=0,month=date.getMonth()|0; i<16384; i=i+1|0) date.setMonth(month=month+1+(dstOffsetAtDate(date)|0)|0); console.timeEnd("Speed of processing 16384 dates");

我相信上面的代码片段优于这里发布的所有其他答案,原因有很多。

This answer works in all time zones, even Antarctica/Casey. Daylight savings is very much subject to change. It might be that 20 years from now, some country might have 3 DST periods instead of the normal 2. This code handles that case by returning the DST offset in milliseconds, not just whether DST is in effect or not in effect. The size of the months of the year and the way that Leap Years work fits perfectly into keeping our time on track with the sun. Heck, it works so perfectly that all we ever do is just adjust mere seconds here and there. Our current system of leap years has been in effect since February 24th, 1582, and will likely stay in effect for the foreseeable future. This code works in timezones that do not use DST. This code works in historic times before when DST was implemented (such as the 1900s). This code is maximally integer-optimized and should give you no problem if called in a tight loop. After running the code snippet above, scroll down to the bottom of the output to see the performance benchmark. My computer is able to process 16384 dates in 29ms on FireFox.

但是,如果您没有为超过2个DST周期做准备,那么可以使用下面的代码来确定DST是否作为布尔值有效。

function isDaylightSavingsInEffect(dateInput) {
    // To satisfy the original question
    return dstOffsetAtDate(dateInput) !== 0;
}

这段代码使用了这样一个事实,即getTimezoneOffset在标准时间与日光节约时间(DST)期间返回更大的值。因此,它确定标准时间内的预期输出,并比较给定日期的输出是否相同(标准)或更少(DST)。

注意,getTimezoneOffset对于UTC以西的区域返回正数分钟,通常表示为负小时(因为它们“落后”UTC)。例如,洛杉矶是UTC-8h标准,UTC-7h夏令时。getTimezoneOffset在12月(冬季,标准时间)返回480(正480分钟),而不是-480。它返回东半球的负数(例如冬季悉尼的-600,尽管这是“超前”(UTC+10h)。

Date.prototype.stdTimezoneOffset = function () {
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}

Date.prototype.isDstObserved = function () {
    return this.getTimezoneOffset() < this.stdTimezoneOffset();
}

var today = new Date();
if (today.isDstObserved()) { 
    alert ("Daylight saving time!");
}

js库在它的time对象上提供了一个. isdst()方法。

moment#isDST检查当前时刻是否属于夏时制。

moment([2011, 2, 12]).isDST(); // false, March 12 2011 is not DST
moment([2011, 2, 14]).isDST(); // true, March 14 2011 is DST

创建两个日期:一个在六月,一个在一月。比较它们的getTimezoneOffset()值。

如果一月抵消>六月抵消,客户端在北半球 如果1月偏移量< 6月偏移量,则客户在南半球 如果没有差异,则客户端时区不遵守夏令时

现在检查当前日期的getTimezoneOffset()。

如果等于北半球的6月,则当前时区为夏时制(+1小时) 如果等于南半球的1月,则当前时区为夏时制(+1小时)

我今天也遇到了同样的问题,但由于我们的夏令时开始和结束的时间与美国不同(至少从我的理解来看),我使用了稍微不同的路线。

var arr = [];
for (var i = 0; i < 365; i++) {
 var d = new Date();
 d.setDate(i);
 newoffset = d.getTimezoneOffset();
 arr.push(newoffset);
}
DST = Math.min.apply(null, arr);
nonDST = Math.max.apply(null, arr);

然后,您只需将当前时区偏移量与DST和非DST进行比较,以查看哪一个匹配。