使用 JavaScript 中的 Date 对象显示亚洲和美国日期时间
javascriptweb developmentobject oriented programming更新于 2025/11/21 22:36:17
为此,您可以使用 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
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

