JS一个根据时区输出时区时间的函数
2015-05-23 07:48
399 查看
做项目遇到的坑爹问题,需要根据时区获取时区中轴线的时间。为此搜了好久网上都没什么JS的代码描述到这一方面,最后自己翻了下高中地理才写了个函数出来。
此图可以看出来,全球分为了0时区,东西1-11区,第12时区。下面就是我写的JS的根据时区输出时间的函数:
这里用到了getTime函数获取1970年1月1日午夜到现在的时间差,配合上现在本地与GMT时间的差来求得GMT时间的标准值。也就是函数里面的utcTime变量。
最后附上结果图:
点击查看原文:原文地址
此图可以看出来,全球分为了0时区,东西1-11区,第12时区。下面就是我写的JS的根据时区输出时间的函数:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>根据时区取得时区时间</title> <meta name="Blog" content="http://www.cnblogs.com/manfredHu/"> <meta name="Author" content="manfredHu"> </head> <body> <script type="text/javascript"> //得到标准时区的时间的函数 function getLocalTime(i) { //参数i为时区值数字,比如北京为东八区则输进8,西5输入-5 if (typeof i !== 'number') return; var d = new Date(); //得到1970年一月一日到现在的秒数 var len = d.getTime(); //本地时间与GMT时间的时间偏移差 var offset = d.getTimezoneOffset() * 60000; //得到现在的格林尼治时间 var utcTime = len + offset; return new Date(utcTime + 3600000 * i); } console.log("*******************东区时间************************************"); console.log("零时区-伦敦时间:" + getLocalTime(0)); console.log("东一区-柏林时间:" + getLocalTime(1)); console.log("东二区-雅典时间:" + getLocalTime(2)); console.log("东三区-莫斯科时间:" + getLocalTime(3)); console.log("东四区-时间:" + getLocalTime(4)); console.log("东五区-伊斯兰堡时间:" + getLocalTime(5)); console.log("东六区-科伦坡时间:" + getLocalTime(6)); console.log("东七区-曼谷时间:" + getLocalTime(7)); console.log("东八区-北京时间:" + getLocalTime(8)); console.log("东九区-东京时间:" + getLocalTime(9)); console.log("东十区-悉尼时间:" + getLocalTime(10)); console.log("东十二区-斐济时间:" + getLocalTime(12)); console.log("*******************西区时间************************************"); console.log("西十区-斐济时间:" + getLocalTime(-10)); console.log("西九区-阿拉斯加时间:" + getLocalTime(-9)); console.log("西八区-太平洋时间(美国和加拿大):" + getLocalTime(-8)); console.log("西七区-山地时间(美国和加拿大):" + getLocalTime(-7)); console.log("西六区-中部时间(美国和加拿大):" + getLocalTime(-6)); console.log("西五区-东部时间(美国和加拿大):" + getLocalTime(-5)); console.log("西四区-大西洋时间(加拿大):" + getLocalTime(-4)); console.log("西三区-巴西利亚时间:" + getLocalTime(-3)); </script> </body> </html>
这里用到了getTime函数获取1970年1月1日午夜到现在的时间差,配合上现在本地与GMT时间的差来求得GMT时间的标准值。也就是函数里面的utcTime变量。
最后附上结果图:
点击查看原文:原文地址
相关文章推荐
- js 绑定时间后,浏览器会根据函数的参数传递一个事件
- NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间
- 一个用JS写的IP/时间/日期/email的验证函数
- JS写一个函数输出今天的日期
- js根据时区修改时间
- [c.y.j]js根据地区时区显示时间
- js如何查看一个函数的执行时间
- js查看一个函数的执行时间实例代码
- js指定一个时间执行一个函数
- 用js实现的一个根据内容自动生成表格的函数
- 编写一个函数,要求输入年月日时分秒,输出下一个时间
- js如何实现一定时间后去执行一个函数
- js查看一个函数的执行时间实例代码
- 一个时间格式的函数,可以把时间格式为各种输出
- 一个可以根据设定输出图片的函数 可以改变图片的大小
- Objective-c 根据某一个时区的时间获取特定时区的时间
- js:制作一个简易的计数器:根据输入的两个整数和运算符,进行计算,然后输出计算结果
- 一个判断抢购时间是否到达的简单的js函数
- 用js实现的一个根据内容自动生成表格的函数
- js查看一个函数的执行所用时间