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


Posted in Javascript onFebruary 23, 2014
<!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>
Javascript 相关文章推荐
javascript下有关dom以及xml节点访问兼容问题
Nov 26 Javascript
SWFObject Flash js调用类
Jul 08 Javascript
高性能web开发 如何加载JS,JS应该放在什么位置?
May 14 Javascript
分享JavaScript获取网页关闭与取消关闭的事件
Dec 13 Javascript
推荐 21 款优秀的高性能 Node.js 开发框架
Aug 18 Javascript
javascript使用数组的push方法完成快速排序
Sep 15 Javascript
全屏js头像上传插件源码高清版
Mar 29 Javascript
centos 上快速搭建ghost博客方法分享
May 23 Javascript
vue 实现左右拖拽元素并且不超过他的父元素的宽度
Nov 30 Javascript
微信小程序模板template简单用法示例
Dec 04 Javascript
jQuery事件多次绑定与解绑问题实例分析
Feb 19 jQuery
JS前端使用Canvas快速实现手势解锁特效
Sep 23 Javascript
js 金额格式化来回转换示例
Feb 23 #Javascript
js实现连个数字相加而不是拼接的方法
Feb 23 #Javascript
jquery $(&quot;#variable&quot;) 循环改变variable的值示例
Feb 23 #Javascript
用JQuery实现全选与取消的两种简单方法
Feb 22 #Javascript
jquery实现checkbox 全选/全不选的通用写法
Feb 22 #Javascript
防止jQuery ajax Load使用缓存的方法小结
Feb 22 #Javascript
jquery中load方法的用法及注意事项说明
Feb 22 #Javascript
You might like
1982年日本摄影师镜头下的中国孩子 那无忧无虑的童年
2020/03/12 杂记
php实现12306火车票余票查询和价格查询(12306火车票查询)
2014/01/14 PHP
PHP的APC模块实现上传进度条
2015/10/27 PHP
firefox中JS读取XML文件
2006/12/21 Javascript
JQuery的自定义事件代码,触发,绑定简单实例
2013/08/01 Javascript
jQuery 绑定事件到动态创建的元素上的方法实例
2013/08/18 Javascript
mac上node.js环境的安装测试
2017/07/03 Javascript
VUE 实现复制内容到剪贴板的两种方法
2019/04/24 Javascript
js全屏事件fullscreenchange 实现全屏、退出全屏操作
2019/09/17 Javascript
python实现的一个p2p文件传输实例
2014/06/04 Python
使用Python实现一个简单的项目监控
2015/03/31 Python
python获取本地计算机名字的方法
2015/04/29 Python
Python使用Supervisor来管理进程的方法
2015/05/28 Python
Python格式化输出%s和%d
2018/05/07 Python
用Python画小女孩放风筝的示例
2019/11/23 Python
如何使用python3获取当前路径及os.path.dirname的使用
2019/12/13 Python
详解Windows下PyCharm安装Numpy包及无法安装问题解决方案
2020/06/18 Python
基于logstash实现日志文件同步elasticsearch
2020/08/06 Python
详解python变量与数据类型
2020/08/25 Python
CSS3 Flexbox中flex-shrink属性的用法示例介绍
2013/12/30 HTML / CSS
CSS3打造百度贴吧的3D翻牌效果示例
2017/01/04 HTML / CSS
html5+svg学习指南之SVG基础知识
2014/12/17 HTML / CSS
Sneaker Studio匈牙利:购买运动鞋
2018/03/26 全球购物
异常和异常类的概念
2014/09/12 面试题
如何打印出当前源文件的文件名以及源文件的当前行号
2015/04/05 面试题
班组长工作职责
2013/12/25 职场文书
善意的谎言事例
2014/02/15 职场文书
革命先烈的英雄事迹材料
2014/02/15 职场文书
经典演讲稿汇总
2014/05/19 职场文书
公司门卫工作职责
2014/06/28 职场文书
在宿舍喝酒的检讨书
2014/09/28 职场文书
《玩出了名堂》教学反思
2016/02/17 职场文书
《神奇的鸟岛》教学反思
2016/02/22 职场文书
关于考试抄袭的检讨书
2019/11/02 职场文书
python基础之while循环语句的使用
2021/04/20 Python
【海涛解说】暗牧也疯狂,牛蛙成配角
2022/04/01 DOTA