使用 JavaScript 中的 Date 物件顯示亞洲和美洲的日期時間
為此,您可以使用 JavaScript 中的 timeZone,即亞洲和美洲特定的時區。
對於亞洲時區
var todayDateTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});
對於美洲時區
var americaDateTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
示例
var todayDateTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"}); todayDateTime = new Date(todayDateTime); console.log("The Asia Date time is="); console.log(todayDateTime) var americaDateTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"}); americaDateTime = new Date(americaDateTime); console.log("The America Date time is="); console.log(americaDateTime);
要執行以上程式,您需要使用以下命令 -
node fileName.js.
此處,我的檔名是 demo193.js。
輸出
這會產生以下輸出 -
PS C:\Users\Amit\javascript-code> node demo193.js The Asia Date time is= 2020-08-08T08:44:50.000Z The America Date time is= 2020-08-07T23:14:50.000Z
廣告