开发工具分享
  • 首页
  • 计算科学
  • 文化旅游
  • 项目和网站
    • OSSEZ 计算技术
    • USRealEstate 社区
    • 地区文化
    • CWIKI.US
    • BUG.OSSEZ.COM
    • RSS.OSSEZ.COM
CWIKIUS.CN
一个有独立思考和温度的清新站
  1. Home
  2. Computer Science
  3. This article

Moment.js 转换 UTC 格式的 2 个小问题

2021年11月15日 605Browse 0Like 0Comments

在使用 Moment.js 转换为 UTC 格式的时候,我们可能会遇到 2 个小问题。

如果你使用

('2021-11-01T19:39:00.000').utc().format();

来进行 UTC 时间转换的话,你会发现你输入时间和输出时间是不同的。

转换时区

如果上面所描述的,在结果中,我们看到如下图

 

moment-01

 

输入的时间,我们实际上使用 UTC 的时间,但是在转换成功后,我们会发现 2 个时间有偏差。

有这个偏差的原因就是当前你的计算机的时间。

从下图我们可以看到:

 

moment-02

 

在对象初始化的时候,是否是 UTC 的时间参数选项是 false。

因此才会出现偏差。

如果你已知的输入时间是 UTC 时间的话,你可以使用下面的代码:

moment.utc('2021-11-01T19:39:00.000').format();

 

moment-03

 

这样的方式初始化对象的话,对象的时间标记将会标记为 UTC 时间。

format 函数没有毫秒

聪明的你可能会看到,如果你使用 format() 函数进行格式化 ISO 时间的话是没有毫秒的。

如果你希望你的时间对象格式化输出后有毫秒数据。

你应该使用的方法是:

toISOString()

考察下面的代码:

moment.utc('2021-11-01T19:39:00.000').toISOString()

如下图所示,下面的代码格式化输出后将会有毫秒数据:

 

moment-04

 

这是在实际使用的时候需要注意一下的地方。

https://www.ossez.com/t/moment-js-utc-2/13807

Tags: None
Last updated:2021年11月15日

HoneyMoose

有温度的人文和独立的思考

Like
< Previous
Next >

Comments

Cancel reply

Archives
  • May 2026
  • April 2026
  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
Categories
  • Computer Science (2,362)
    • Confluence (663)
    • Gradle (12)
  • U.S. (482)
  • 文化旅游 (145)

COPYRIGHT © 2020 CWIKIUS. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

湘ICP备2020018253号-1