Created
November 25, 2013 05:19
-
-
Save kejyun/7636723 to your computer and use it in GitHub Desktop.
JavaScript 時間時區轉換
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 新增當地時區的時間物件 | |
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
这个是错误的
详情见:https://stackoverflow.com/questions/8047616/get-a-utc-timestamp-in-javascript