gpt4 book ai didi

javascript - 使用 Luxon 格式化 ISO 时间

转载 作者:行者123 更新时间:2023-12-04 14:58:35 26 4
gpt4 key购买 nike

使用 乐讯JS ,我一直在尝试使用 native 将日期时间格式化为以某种格式输出符合 ISO 功能:
这就是我得到的:

"2018-08-25T09:00:40.000-04:00"
这就是我想要的:
"2018-08-25T13:00:40.000Z"
我知道它们在unix时间方面都是等价的,除了格式不同之外,它们的含义相同,我只想能够输出第二个字符串而不是第一个字符串。我浏览了 Luxon 文档,但找不到任何可以满足我需要的参数/选项。

最佳答案

正如评论中已经说明的其他内容,您可以使用两种方法:

  • 使用 toUTC 将 Luxon DateTime 转换为 UTC :
    "Set" the DateTime's zone to UTC. Returns a newly-constructed DateTime.
  • 使用 toISOString() JS 日期的方法。

  • 您可以使用 toJSDate() 从 luxon DateTime 获取 Date 对象:
    Returns a JavaScript Date equivalent to this DateTime.

    例子:

    const DateTime = luxon.DateTime;
    const dt = DateTime.now();
    console.log(dt.toISO())
    console.log(dt.toUTC().toISO())
    console.log(dt.toJSDate().toISOString())
    console.log(new Date().toISOString())
    <script src="https://cdn.jsdelivr.net/npm/luxon@1.26.0/build/global/luxon.js"></script>

    关于javascript - 使用 Luxon 格式化 ISO 时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67407325/

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