Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fallingfree/9efb5b2e08462c7ff97f to your computer and use it in GitHub Desktop.
Save fallingfree/9efb5b2e08462c7ff97f to your computer and use it in GitHub Desktop.
// 新增當地時區的時間物件
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());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment