Skip to content

Instantly share code, notes, and snippets.

@kejyun
Created November 25, 2013 05:19
Show Gist options
  • Save kejyun/7636723 to your computer and use it in GitHub Desktop.
Save kejyun/7636723 to your computer and use it in GitHub Desktop.
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());
@zqingr
Copy link

zqingr commented Apr 23, 2018

// 取得 UTC time
utc = d.getTime() + (d.getTimezoneOffset() * 60000);

这个是错误的

详情见:https://stackoverflow.com/questions/8047616/get-a-utc-timestamp-in-javascript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment