It’s perhaps easiest to think of JavaScript time existing in four parts:
可能最容易想到存在四个部分JavaScript时间:
- Microsecond time from the internal system clock, used in the Web Performance API. Web Performance API中使用的距内部系统时钟的微秒时间。
- The “system-level” time of milliseconds and Unix time, on which the others ultimately depend. 其他人最终依赖的“系统级”时间(毫秒)和Unix时间。
Timers:
setTimeout
andsetInterval
, which typically set delays of milliseconds to seconds.计时器:
setTimeout
和setInterval
,通常将毫秒延迟设置为秒。- “Human-readable” date formats, such as “January 1, 2015”. “人类可读”的日期格式,例如“ 2015年1月1日”。
JavaScript doesn’t make these distinctions itself: with the exception of the Web Performance API, all time in JavaScript is derived from a standard known as the Unix epoch.
JavaScript本身并没有做出这些区分:除Web Performance API之外,JavaScript中的所有时间都源自一个称为Unix时代的标准。
JavaScript时间约定 (JavaScript time conventions)
Most every expression of time on a digital device is derived from the microsecond “tick-tock” of a chip, which keeps track of the so-called Unix epoch: the number of milliseconds that have passed since midnight, January 1, 1970 UCT. You can see this number for yourself in a browser: open up a console window and type:
数字设备上的大多数时间表达都来自芯片的微秒“滴答”声,它跟踪所谓的Unix时期:自1970年1月1日午夜以来经过的毫秒数。 您可以在浏览器中自己看到此数字:打开控制台窗口,然后键入:
Date.now()
The number you’ll get back after hitting Enter look something like this: > 1413067397613
按下Enter键后您会得到的号码如下所示:> 1413067397613
This ever-increasing number, also known as the Unix timestamp, is the means by which most computer time is measured, with everything else – time in the future, past, and the difference between dates – resolved by conversion into this format, after taking into account other factors such as time zones and leap seconds.
这个不断增加的数字(也称为Unix时间戳)是用来测量大多数计算机时间的方法,其他所有信息(将来,过去的时间以及日期之间的差额)都可以通过转换为这种格式来解决,考虑其他因素,例如时区和leap秒。
High resolution (1 millionth of a second) timing is also available via the Web Performance API, although that is excessive for most purposes.
还可以通过Web Performance API获得高分辨率(百万分之一秒)的定时,尽管对于大多数目的来说,这是多余的。
This series will explore JavaScript time from the top down, starting with the human-readable formats and working to the micro level. We’ll start by making a simple clock, progressing to more advanced examples.
本系列将从上至下探索JavaScript时间,从人类可读的格式开始,再到微观层次。 我们将首先制作一个简单的时钟,然后继续进行更高级的示例。
翻译自: https://thenewcode.com/940/Introduction-to-Dates-and-Times-in-JavaScript