40.日期(Date

原文: http://exploringjs.com/impatient-js/ch_dates.html

本章介绍 JavaScript 用于处理日期的 API - Date

40.1。最佳实践:不要使用当前的内置 API

JavaScript Date API 使用起来很麻烦。因此,最好依靠图书馆来处理与日期相关的任何事情。热门图书馆包括:

请参阅博客文章“为什么你不应该使用 Moment.js ......”了解这些库的优缺点。

此外,TC39 正在开发 JavaScript 的新日期 API: temporal

40.2。背景:UTC 与 GMT

UTC(协调世界时)和 GMT(格林威治标准时间)具有相同的当前时间,但它们是不同的东西:

  • UTC:是所有时区所基于的时间标准。它们是相对于它指定的。也就是说,没有状态或地区将 UTC 作为其本地时区。

  • GMT:是一些欧洲和非洲状态使用的时区。

资料来源:“TimeTndDate.com 上的 GMT 和 UTC 之间的差异”

40.3。背景:日期时间格式(ISO)

日期时间格式描述:

  • 接受的字符串:
    • Date.parse()
    • new Date()
  • 返回的字符串(总是最长的格式):
    • Date.prototype.toISOString()

以下是.toISOString()返回的日期时间字符串的示例:

'2033-05-28T15:59:59.123Z'

日期时间格式具有以下结构:

  • 日期格式:Y =年份; M =月; d =天

    YYYY-MM-DD
    YYYY-MM
    YYYY
    
  • 时间格式:T =分隔符(字符串'T'); H =小时; M =分钟; s =秒和毫秒; Z =时区是 UTC(字符串'Z'

    THH:mm:ss.sss
    THH:mm:ss.sssZ
    
    THH:mm:ss
    THH:mm:ssZ
    
    THH:mm
    THH:mmZ
    
  • 日期时间格式:日期格式后跟时间格式。

    • 例如(最长):YYYY-MM-DDTHH:mm:ss.sssZ

Z的替代方案 - 相对于 UTC 的时区:

  • +hh:mm
  • -hh:mm

40.4。时间值

_ 时间值 _ 表示自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数的日期。

时间值可用于创建日期:

const timeValue = 0;
assert.equal(
  new Date(timeValue).toISOString(),
  '1970-01-01T00:00:00.000Z');

将日期强制转换为数字会返回其时间值:

> Number(new Date(123))
123

订购运算符将他们的操作数强制转换为数字。因此,您可以使用这些运算符来比较日期:

assert.equal(new Date('1972-05-03') < new Date('2001-12-23'), true);
// Internally:
assert.equal(73699200000 < 1009065600000, true);

40.4.1。创建时间值

以下方法创建时间值:

  • Date.now(): number

    返回当前时间作为时间值。

  • Date.parse(dateTimeString): number(当地时区)

    解析dateTimeString并返回相应的时间值。

  • Date.UTC(year, month, date?, hours?, minutes?, seconds?, milliseconds?): number

    返回指定的 UTC 日期时间的时间值。

40.4.2。获取和设置时间值

  • Date.prototype.getTime(): number

    返回与 Date 对应的时间值。

  • Date.prototype.setTime(timeValue)

    this设置为timeValue编码的日期。

40.5。创建日期

  • new Date(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, milliseconds?: number)(当地时区)

    > new Date(2077,0,27, 21,49,58, 888).toISOString() // CET (UTC+1)
    '2077-01-27T20:49:58.888Z'
    
  • new Date(dateTimeStr: string)(UTC)

    > new Date('2077-01-27').toISOString()
    '2077-01-27T00:00:00.000Z'
    
  • new Date(timeValue: number)

    > new Date(0).toISOString()
    '1970-01-01T00:00:00.000Z'
    
  • new Date()(与new Date(Date.now())相同)

40.6。吸气剂和二传手

40.6.1。时间单位吸气剂和二传手

日期有时间单位的 getter 和 setter。例如:

  • Date.prototype.getFullYear()
  • Date.prototype.setFullYear(num)

这些 getter 和 setter 符合以下模式:

  • 当地时间:
    • Date.prototype.get«Unit»()
    • Date.prototype.set«Unit»(num)
  • 世界时间:
    • Date.prototype.getUTC«Unit»()
    • Date.prototype.setUTC«Unit»(num)

这些是支持的时间单位:

  • 日期
    • FullYear
    • Month:月(0-11)。 陷阱: 0 是 1 月等
    • Date:每月的某一天(1-31)
    • Day(仅限吸气剂):星期几(0-6); 0 是星期天
  • 时间
    • Hours:小时(0-23)
    • Minutes:分钟(0-59)
    • Seconds:秒(0-59)
    • Milliseconds:毫秒(0-999)

还有一个吸气剂不符合前面提到的模式:

  • Date.prototype.getTimezoneOffset()

    返回本地时间和 UTC 之间的时差,以分钟为单位。例如,对于 CET,它返回-60

40.7。将日期转换为字符串

示例日期:

const d = new Date(0);

40.7.1。随着时间的推移

  • Date.prototype.toTimeString()(当地时区)

    > d.toTimeString()
    '01:00:00 GMT+0100 (Central European Standard Time)'
    
  • Date.prototype.toLocaleTimeString()(参见 ECMAScript 国际化 API

40.7.2。带日期的字符串

  • Date.prototype.toDateString()(当地时区)

    > d.toDateString()
    'Thu Jan 01 1970'
    
  • Date.prototype.toLocaleDateString()ECMAScript 国际化 API

40.7.3。带有日期和时间的字符串

  • Date.prototype.toString()(当地时区)

    > d.toString()
    'Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)
    
  • Date.prototype.toLocaleString()(参见 ECMAScript 国际化 API

  • Date.prototype.toUTCString()(UTC)

    > d.toUTCString()
    'Thu, 01 Jan 1970 00:00:00 GMT'
    
  • Date.prototype.toISOString()(UTC)

    > d.toISOString()
    '1970-01-01T00:00:00.000Z'
    

练习:创建日期字符串

exercises/dates/create_date_string_test.js

40.8。 Date API 的陷阱

  • 您无法指定本地时区。如果您不小心,这可能会导致特定于位置的错误。例如,以下交互会导致不同的结果,具体取决于执行的位置。我们在 CET 中执行它,这就是为什么 ISO 字符串(UTC)具有不同的一天(26 对 27)。

    > new Date(2077, 0, 27).toISOString()
    '2077-01-26T23:00:00.000Z'
    
  • 1 月是 0,2 月是 1,等等:

    > new Date(1979, 0, 27, 10, 30).toISOString()
    '1979-01-27T09:30:00.000Z'
    
  • 多年y0≤y≤99,增加了 1900:

    > new Date(12, 1, 22, 19, 11).toISOString()
    '1912-02-22T18:11:00.000Z'
    

书籍推荐