js 根据时区转换日期格式

本文介绍了一种使用JavaScript进行全球各时区时间转换的方法,通过一个简单的函数getLocalTime,可以快速获取从零时区到东十二区及西十区的当前时间,适用于需要处理多时区数据的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js 各个时区时间转换

//得到标准时区的时间的函数

var day = new Date();
getLocalTime(8,day)    		// 北京时间
function getLocalTime(i) {
 
    //参数i为时区值数字,比如北京为东八区则输进8,西5输入-5
 
    if (typeof i !== 'number') return;
 
    //得到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));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值