Photo by Mohammad Rahmani on Unsplash
JavaScript时间时区转换
// 新增当地时区的时间物件
function DateTimezone(offset) {
// 建立现在时间的物件
d = new Date();
// 取得 UTC time
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// 新增不同时区的日期资料
return new Date(utc + (3600000*offset));
}
// 计算当地时区的时间
function calcTime(city, offset) {
// 建立现在时间的物件
d = new Date();
// 取得 UTC time
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// 新增不同时区的日期资料
nd = new Date(utc + (3600000*offset));
// 显示当地时间
return "在 " + city + " 的本地时间是 " + nd.toLocaleString();
}
console.log(calcTime('taipei' , 8));
var date_taipei = DateTimezone(8);
console.log(date_taipei.toLocaleString());
Donate KJ 贊助作者喝咖啡
如果這篇文章對你有幫助的話,可以透過下面支付方式贊助作者喝咖啡,如果有什麼建議或想說的話可以贊助並留言給我
If this article has been helpful to you, you can support the author by treating them to a coffee through the payment options below. If you have any suggestions or comments, feel free to sponsor and leave a message for me!
方式 Method | 贊助 Donate |
PayPal | https://paypal.me/kejyun |
綠界 ECPay | https://p.ecpay.com.tw/AC218F1 |
歐付寶 OPay | https://payment.opay.tw/Broadcaster/Donate/BD2BD896029F2155041C8C8FAED3A6F8 |