UTC vs ISO格式的时间 [英] UTC vs ISO format for time
问题描述
我试图了解UTC和ISO格式之间的区别,以及何时在服务器之间传输消息时使用什么格式.所以当我尝试以下内容时,这就是我所得到的
I'm trying to understand the difference between UTC and ISO formats and when to use what when transferring messages between servers. So When I try the following this is what I get
new Date().toISOString()
"2019-11-14T00:55:31.820Z"
new Date().toUTCString()
"Thu, 14 Nov 2019 00:55:16 GMT"
我了解ISO格式及其用于表示时间的标准,但是UTC的目的是什么,我将在哪里使用它们?
I understand the ISO format and its a standard used to represent time, but what is the purpose of UTC and where would I use them?
推荐答案
tl; dr
- 始终使用 ISO 8601 格式:
2019-11-14T00:55:31.820Z
- 避免使用RFC 1123&的旧版格式 822:
Thu, 14 Nov 2019 00:55:16 GMT
- Always use ISO 8601 format:
2019-11-14T00:55:31.820Z
- Avoid the legacy format of RFC 1123 & 822:
Thu, 14 Nov 2019 00:55:16 GMT
tl;dr
UTC和GMT不是格式.
UTC and GMT are not formats.
UTC 和
UTC and GMT are two slightly different ways of tracking time. This is a complicated topic, so see the Wikipedia pages for the gory details if you really want to know.
对于常见的商务应用程序,没有显着差异,字面上的差异不到一秒钟.大多数程序员可以互换使用这些术语.如果您为NASA或GPS/Galileo导航项目工作,那么您将需要了解更多信息.
For common business apps, there is no significant difference, literally less than a second’s difference. Most programmers can use the terms interchangeably. If you work for NASA, or the GPS/Galileo navigation projects, then you’ll want to learn more.
在第一个示例2019-11-14T00:55:31.820Z
中看到的格式由 ISO 8601 标准定义.中间的T
将年月日部分与时分秒部分分开.最后的Z
表示UTC,即相对于UTC的偏移量为零小时-分钟-秒.根据军事/航空传统,Z
发音为"Zulu".
The format seen in your first example 2019-11-14T00:55:31.820Z
is defined by the ISO 8601 standard. The T
in the middle separates the year-month-day portion from the hour-minute-second portion. The Z
on the end means UTC, that is, an offset-from-UTC of zero hours-minutes-seconds. The Z
is pronounced "Zulu" per military/aviation tradition.
ISO 8601标准更加现代化.格式经过精心设计,易于通过机器解析,也易于人类跨文化阅读.
The ISO 8601 standard is more modern. The formats are wisely designed to be easy to parse by machine as well as easy to read by humans across cultures.
始终选择ISO 8601 .
第二个示例字符串Thu, 14 Nov 2019 00:55:16 GMT
是在较早的标准 RFC 1123 中定义的. RFC 822 .
Your second example string Thu, 14 Nov 2019 00:55:16 GMT
is defined in the older standards RFC 1123 & RFC 822.
这些是旧版格式.它们很糟糕,很难用机器解析.它们对人类有害,因为他们假设英语和特定的文化规范.
These are legacy formats. They are terrible, difficult to parse by machine. And they are bad for humans as they assume English language and particular cultural norms.
避免使用此格式.仅当对旧协议和尚未针对ISO 8601更新的系统需要时才使用此功能.
Avoid this format whenever possible. Use this only when required for old protocols and systems not yet updated for ISO 8601.
您的2019-11-14T00:55:31.820Z
示例表示相对于UTC的零时分分钟秒.这是站在格林威治皇家天文台.
Your example of 2019-11-14T00:55:31.820Z
means an offset from UTC of zero hours-minutes seconds. This is the time-of-day and date seen when standing before the clock displayed at the Royal Observatory Greenwich.
(图片来源)
一个小时后,与在突尼斯墙上的钟表上看到的同时发生的时刻显示了一个小时:2019-11-14T01:55:31.820 + 01:00 [非洲/突尼斯].正如+01:00
所指出的,突尼斯Africa/Tunis
的时区当时是UTC的提前一个小时 .
That same simultaneous moment as seen on the clocks hanging on the wall in Tunisia show an hour later: 2019-11-14T01:55:31.820+01:00[Africa/Tunis]. The time zone of Tunisia Africa/Tunis
is one hour ahead of UTC at that moment, as noted by the +01:00
.
与魁北克墙上墙上的钟表所显示的同时发生的同一时刻显示了之前日期的近8点:2019-11-13T19:55:31.820-05:00 [美国/蒙特利尔].正如-05:00
所指出的,魁北克America/Montreal
的时区当时比UTC落后 5小时.
That same simultaneous moment as seen on the clocks hanging on the walls in Québec show nearly 8 PM of the prior date: 2019-11-13T19:55:31.820-05:00[America/Montreal]. The time zone of Québec America/Montreal
is five hours behind UTC at that moment, as noted by the -05:00
.
您可以看到这些计算是通过Java代码(不是您的问题中标记的JavaScript)在IdeOne.com上实时运行 /a>.
You can see these calculations being made with Java code (not JavaScript as tagged on your Question) running live at IdeOne.com.
通常最好在UTC中进行大部分思考,业务逻辑,数据存储,数据交换和登录.仅在业务规则要求并向用户显示值时,才调整为时区.
Generally best to do most of your thinking, business logic, data storage, data exchange, and logging in UTC. Adjust to a time zone only when required by business rules, and when presenting values to a user.
这篇关于UTC vs ISO格式的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!