繁体   English   中英

如何从 JavaScript 中的 UTC 日期获取全天和月份名称

[英]How to get full day & month names from UTC date in JavaScript

我正在尝试使用 JavaScript 将存储为 JSON 文件中的字符串的日期转换为 UTC 字符串格式。

这是我的 JSON:

{
    "createdDate": {
        "utc": "2022-08-20T12:18:48.6588096Z",
        "local": "2022-08-20T12:18:48.6588096"
    }
}

这是我正在尝试使用的 function,但我不确定它为什么显示 GMT:

let createdByUtc = new Date(resp.createdDate.utc).toUTCString()

实际结果:

格林威治标准时间 2022 年 8 月 20 日星期六 12:08:52

要求的结果:

UTC 时间 2022 年 8 月 20 日星期六下午 12:08

有人可以告诉我如何获得所需格式的createdByUtc变量吗?

使用具有适当日期和时间 styles 的 UTC 表示的配置的Intl.DateTimeFormat实例:

 const {format} = new Intl.DateTimeFormat('en-US', { timeZone: 'UTC', dateStyle: 'full', timeStyle: 'long' } ); // Example let dt = new Date("2022-08-20T12:18:48.6588096Z"); console.log(format(dt));

尝试定义一些选项,如下所示。

let createdByUtc = JSON.parse( '{"utc": "2022-08-20T12:18:48.6588096Z", "local": "2022-08-20T12:18:48.6588096"}');
var newDate =new Date(createdByUtc.utc)

const options = {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour:'numeric',
  minute:'numeric'
};
options.timeZone = 'UTC';
options.timeZoneName = 'short';
console.log(newDate.toLocaleString('en-US', options))

//Saturday, August 20, 2022, 12:18 PM UTC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM