Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Javascript

แสดงเวลาวันที่ในเอเชียและอเมริกันด้วยออบเจ็กต์วันที่ใน JavaScript


สำหรับสิ่งนี้ คุณสามารถใช้ timeZone จาก JavaScript เช่น เขตเวลาเฉพาะสำหรับเอเชียและอเมริกาตามลำดับ

สำหรับโซนเวลาเอเชีย

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