Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Hiển thị ngày giờ Châu Á và Châu Mỹ với Đối tượng Date trong JavaScript

Để làm điều này, bạn có thể sử dụng timeZone từ JavaScript, tức là múi giờ cụ thể cho Châu Á và Châu Mỹ tương ứng.

Đối với Múi giờ Châu Á

var todayDateTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});

Đối với Múi giờ Châu Mỹ

var americaDateTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});

Ví dụ

var todayDateTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});
todayDateTime = new Date(todayDateTime);
console.log("Ngày giờ Châu Á là=");
console.log(todayDateTime)
var americaDateTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
americaDateTime = new Date(americaDateTime);
console.log("Ngày giờ Châu Mỹ là=");
console.log(americaDateTime);

Để chạy chương trình trên, bạn cần sử dụng lệnh sau:

node fileName.js

Tên tệp của tôi là demo193.js.

Kết quả

Chương trình sẽ cho ra kết quả sau:

PS C:\Users\Amit\javascript-code> node demo193.js
Ngày giờ Châu Á là= 2020-08-08T08:44:50.000Z
Ngày giờ Châu Mỹ là= 2020-08-07T23:14:50.000Z