永发信息网

js获得当前时区夏令时发生和终止的时间代码

答案:1  悬赏:60  手机版
解决时间 2021-10-16 05:27
js获得当前时区夏令时发生和终止的时间代码
最佳答案
这篇文章主要介绍了js获得当前时区夏令时发生和终止的时间代码,需要的朋友可以参考下 代码如下: <!DOCTYPE html> <html> <head> <title>DST Calculator</title> <script type="text/javascript"> function DisplayDstSwitchDates() { var year = new Date().getYear(); if (year < 1000) year += 1900; var firstSwitch = 0; var secondSwitch = 0; var lastOffset = 99; // Loop through every month of the current year for (i = 0; i < 12; i++) { // Fetch the timezone value for the month var newDate = new Date(Date.UTC(year, i, 0, 0, 0, 0, 0)); var tz = -1 * newDate.getTimezoneOffset() / 60; // Capture when a timzezone change occurs if (tz > lastOffset) firstSwitch = i-1; else if (tz < lastOffset) secondSwitch = i-1; lastOffset = tz; } // Go figure out date/time occurences a minute before // a DST adjustment occurs var secondDstDate = FindDstSwitchDate(year, secondSwitch); var firstDstDate = FindDstSwitchDate(year, firstSwitch); if (firstDstDate == null && secondDstDate == null) return 'Daylight Savings is not observed in your timezone.'; else return 'Last minute before DST change occurs in ' + year + ': ' + firstDstDate + ' and ' + secondDstDate; } function FindDstSwitchDate(year, month) { // Set the starting date var baseDate = new Date(Date.UTC(year, month, 0, 0, 0, 0, 0)); var changeDay = 0; var changeMinute = -1; var baseOffset = -1 * baseDate.getTimezoneOffset() / 60; var dstDate; // Loop to find the exact day a timezone adjust occurs for (day = 0; day < 50; day++) { var tmpDate = new Date(Date.UTC(year, month, day, 0, 0, 0, 0)); var tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60; // Check if the timezone changed from one day to the next if (tmpOffset != baseOffset) { var minutes = 0; changeDay = day; // Back-up one day and grap the offset tmpDate = new Date(Date.UTC(year, month, day-1, 0, 0, 0, 0)); tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60; // Count the minutes until a timezone chnage occurs while (changeMinute == -1) { tmpDate = new Date(Date.UTC(year, month, day-1, 0, minutes, 0, 0)); tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60; // Determine the exact minute a timezone change // occurs if (tmpOffset != baseOffset) { // Back-up a minute to get the date/time just // before a timezone change occurs tmpOffset = new Date(Date.UTC(year, month, day-1, 0, minutes-1, 0, 0)); changeMinute = minutes; break; } else minutes++; } // Add a month (for display) since JavaScript counts // months from 0 to 11 dstDate = tmpOffset.getMonth() + 1; // Pad the month as needed if (dstDate < 10) dstDate = "0" + dstDate; // Add the day and year dstDate += '/' + tmpOffset.getDate() + '/' + year + ' '; // Capture the time stamp tmpDate = new Date(Date.UTC(year, month, day-1, 0, minutes-1, 0, 0)); dstDate += tmpDate.toTimeString().split(' ')[0]; return dstDate; } } } </script> </head> <body> <script type="text/javascript"> document.write("Current date/time: " + new Date() + "<br />"); document.write(DisplayDstSwitchDates()); </script> </body> </html>
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
中国农业银行重庆北碚区歇马支行地址在哪,我
有人以说下这个脸上有斑,用阿奴面膜有用吧?
装修的话,事先安装净水系统还是先安装燃气系
有没有谁能来说说什么是经适房和限价房 怎么
卵子排出后不受精会怎么样?
请问大家一下,深圳与上海的消费水平、工资水
苏州哪家丝巾店又便宜质量又好?
方臣、govos、尊威都是假牌子,都是在网上卖
冬天没暖气咋办谁能告诉我
哈密瓜在日本的售价比中国市场高出多少?
新白娘子传奇的导演和编剧是谁呀?他们有没有
大红酸枝五斗橱的价格高吗?大红酸枝五斗橱好
7个月宝宝睡姿哪种好,侧睡好还是平躺睡好,
微信发红包,每天金额上限是多少?
个人买房贷款需要哪些手续?需要什么条件呢
推荐资讯
我是歌手dj郑洋资料简介
我女朋友与我分手了!
纳米润滑油
4平方房间装修怎么样好看?如何装修最省钱
谁有上菱冰箱压缩机啊
慈溪家具厂哪个做得好?
电动喜羊羊摇摇车插电不坐耗电吗
城市高杆灯怎么设计比较好?谁能够指点一下?
在汽车离合器的正确使用方法,尤其在转弯的过
用橘子皮泡水对身体有好处吗?
六枝博大医院办公地址在什么地方,我要处理点
我想求助大家农村客厅吊灯应该怎么装修?有谁
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?