gpt4 book ai didi

javascript - 在 UTC 中调用 toLocaleString()

转载 作者:行者123 更新时间:2023-11-29 16:43:54 25 4
gpt4 key购买 nike

我有一个 d 类型的变量 Date 和值 '2017-05-01T01:00:00.000Z'

我的时区是 UTC-4。

当我调用时:

 d.toLocaleString("en-US", { month: "short" });

我得到 Apr,因为日期是 2017 年 4 月 30 日 2100h UTC-4,我的 Javascript 正在运行。

有没有一种简单的方法可以让 toLocaleString 根据 UTC 等效日期来处理日期实例?

最佳答案

您可以 specify a parameter timeZone ,它定义了格式化日期时要使用的目标时区:

var date = new Date('2017-05-01T01:00:00.000Z');

console.log(date.toLocaleString("en-US", { month: "short", timeZone: 'America/New_York' }));
// "Apr"

console.log(date.toLocaleString("en-US", { month: "short", timeZone: 'UTC' }));
// "May"

关于javascript - 在 UTC 中调用 toLocaleString(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42956104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com