javascript的date操作使用

5次阅读

共计 8213 个字符,预计需要花费 21 分钟才能阅读完成。

Date
Date 对象用于处理日期与时间。
构造方法
new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
参数:
value
代表自 1970 年 1 月 1 日 00:00:00 (世界标准时间) 起经过的毫秒数。
dateString
表示日期的字符串值。该字符串应该能被 Date.parse() 方法识别(符合 IETF-compliant RFC 2822 timestamps 或 version of ISO8601)。
year
代表年份的整数值。为了避免 2000 年问题最好指定 4 位数的年份; 使用 1998, 而不要用 98.
month
代表月份的整数值从 0(1 月)到 11(12 月)。
day
代表一个月中的第几天的整数值,从 1 开始。
hour
代表一天中的小时数的整数值 (24 小时制)。
minute
分钟数。
second
秒数。
millisecond
表示时间的毫秒部分的整数值。
需要注意的是只能通过调用 Date 构造函数来实例化日期对象:以常规函数调用它(即不加 new 操作符)将会返回一个字符串,而不是一个日期对象。另外,不像其他 JavaScript 类型,Date 对象没有字面量格式。当 Date 作为构造函数调用并传入多个参数时,如果数值大于合理范围时(如月份为 13 或者分钟数为 70),相邻的数值会被调整。比如 new Date(2013, 13, 1)等于 new Date(2014, 1, 1),它们都表示日期 2014-02-01(注意月份是从 0 开始的)。其他数值也是类似,new Date(2013, 2, 1, 0, 70)等于 new Date(2013, 2, 1, 1, 10),都表示时间 2013-03-01T01:10:00。

如果没有输入任何参数,则 Date 的构造器会依据系统设置的当前时间来创建一个 Date 对象。
如果提供了至少两个参数,其余的参数均会默认设置为 1(如果没有提供 day 参数)或者 0。
JavaScript 的时间是由世界标准时间(UTC)1970 年 1 月 1 日开始,用毫秒计时,一天由 86,400,000 毫秒组成。Date 对象的范围是 -100,000,000 天至 100,000,000 天(等效的毫秒值)。
JavaScript 的 Date 对象为跨平台提供了统一的行为。时间属性可以在不同的系统中表示相同的时刻,而如果使用了本地时间对象,则反映当地的时间。
JavaScript 的 Date 对象提供了数个 UTC 时间的方法,也相应提供了当地时间的方法。UTC,也就是我们所说的格林威治时间,指的是 time 中的世界时间标准。而当地时间则是指执行 JavaScript 的客户端电脑所设置的时间。
以一个函数的形式来调用 JavaScript 的 Date 对象(i.e., 不使用 new 操作符)会返回一个代表当前日期和时间的字符串。

dateString
表示日期的字符串值。该字符串应该能被 Date.parse() 方法识别(符合 IETF-compliant RFC 2822 timestamps 或 version of ISO8601)。所以需要确认 datestring 的类型。
IETF-compliant RFC 2822 timestamps
最初的 datestring 的只支持:时间字符串
Date and Time Specification

Date and time occur in several header fields. This section specifies
the syntax for a full date and time specification. Though folding
white space is permitted throughout the date-time specification, it
is RECOMMENDED that a single space be used in each place that FWS
appears (whether it is required or optional); some older
implementations may not interpret other occurrences of folding white
space correctly.

date-time = [day-of-week “,”] date FWS time [CFWS]

day-of-week = ([FWS] day-name) / obs-day-of-week

day-name = “Mon” / “Tue” / “Wed” / “Thu” /
“Fri” / “Sat” / “Sun”

date = day month year

year = 4*DIGIT / obs-year

month = (FWS month-name FWS) / obs-month

month-name = “Jan” / “Feb” / “Mar” / “Apr” /
“May” / “Jun” / “Jul” / “Aug” /
“Sep” / “Oct” / “Nov” / “Dec”

day = ([FWS] 1*2DIGIT) / obs-day

time = time-of-day FWS zone

time-of-day = hour “:” minute [“:” second]

hour = 2DIGIT / obs-hour

minute = 2DIGIT / obs-minute

second = 2DIGIT / obs-second

zone = ((“+” / “-”) 4DIGIT) / obs-zone

The day is the numeric day of the month. The year is any numeric
year 1900 or later.

The time-of-day specifies the number of hours, minutes, and
optionally seconds since midnight of the date indicated.

The date and time-of-day SHOULD express local time.

The zone specifies the offset from Coordinated Universal Time (UTC,
formerly referred to as “Greenwich Mean Time”) that the date and
time-of-day represent. The “+” or “-” indicates whether the
time-of-day is ahead of (i.e., east of) or behind (i.e., west of)
Universal Time. The first two digits indicate the number of hours
difference from Universal Time, and the last two digits indicate the
number of minutes difference from Universal Time. (Hence, +hhmm
means +(hh * 60 + mm) minutes, and -hhmm means -(hh * 60 + mm)
minutes). The form “+0000” SHOULD be used to indicate a time zone at
Universal Time. Though “-0000” also indicates Universal Time, it is
used to indicate that the time was generated on a system that may be
in a local time zone other than Universal Time and therefore
indicates that the date-time contains no information about the local
time zone.

A date-time specification MUST be semantically valid. That is, the
day-of-the-week (if included) MUST be the day implied by the date,
the numeric day-of-month MUST be between 1 and the number of days
allowed for the specified month (in the specified year), the
time-of-day MUST be in the range 00:00:00 through 23:59:60 (the
number of seconds allowing for a leap second; see [STD12]), and the
zone MUST be within the range -9959 through +9959.
所以可以得出结论:最开始的 js 仅支持以下格式的 datestring【需要注意的是各个浏览器实现不一致,所以可能会支持其他的格式的 datestring】
Sun 30 Dec 2018 19:42:44 +0800
看起来和 console.log 里面的输出有一点不一致;
Sun Dec 30 2018 19:42:44 GMT+0800 (中国标准时间)

version of ISO8601
在 es5 开始,添加了 ISO8601 格式的支持;
Date Time String Format
ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

Where the fields are as follows:

YYYY is the decimal digits of the year 0000 to 9999 in the Gregorian calendar.

–“-”(hyphen) appears literally twice in the string.

MM is the month of the year from 01 (January) to 12 (December).

DD is the day of the month from 01 to 31.

T“T”appears literally in the string, to indicate the beginning of the time element.

HH is the number of complete hours that have passed since midnight as two decimal digits from 00 to 24.

:“:”(colon) appears literally twice in the string.

mm is the number of complete minutes since the start of the hour as two decimal digits from 00 to 59.

ss is the number of complete seconds since the start of the minute as two decimal digits from 00 to 59.

.“.”(dot) appears literally in the string.

sss is the number of complete milliseconds since the start of the second as three decimal digits.

Z is the time zone offset specified as“Z”(for UTC) or either“+”or“-”followed by a time expression HH:mm

This format includes date-only forms:

YYYY
YYYY-MM
YYYY-MM-DD
It also includes“date-time”forms that consist of one of the above date-only forms immediately followed by one of the following time forms with an optional time zone offset appended:

THH:mm
THH:mm:ss
THH:mm:ss.sss
All numbers must be base 10. If the MM or DD fields are absent“01”is used as the value. If the HH, mm, or ss fields are absent“00”is used as the value and the value of an absent sss field is“000”. The value of an absent time zone offset is“Z”.

Illegal values (out-of-bounds as well as syntax errors) in a format string means that the format string is not a valid instance of this format.

NOTE 1 As every day both starts and ends with midnight, the two notations 00:00 and 24:00 are available to distinguish the two midnights that can be associated with one date. This means that the following two notations refer to exactly the same point in time: 1995-02-04T24:00 and 1995-02-05T00:00

NOTE 2 There exists no international standard that specifies abbreviations for civil time zones like CET, EST, etc. and sometimes the same abbreviation is even used for two very different time zones. For this reason, ISO 8601 and this format specifies numeric representations of date and time.
datestring 的格式可以是如下:

[date]
[date]T[time]
[date]T[time]Z
[date]Ttime[0800]

其中 [date] 可以是:

YYYY
YYYY-MM
YYYY-MM-DD

而 [time] 可以是一下:

THH:mm
THH:mm:ss
THH:mm:ss.sss

可以说 [date] 是必须有的,其他的都是可以没有,会取默认值,当然各个浏览器内部实现的不一致,所以可能还会出现不一致的问题;
API 列表
Date.UTC()
Date.now()
Date.parse()
Date.prototype.getDate()
Date.prototype.getDay()
Date.prototype.getFullYear()
Date.prototype.getHours()
Date.prototype.getMilliseconds()
Date.prototype.getMinutes()
Date.prototype.getMonth()
Date.prototype.getSeconds()
Date.prototype.getTime()
Date.prototype.getTimezoneOffset()
Date.prototype.getUTCDate()
Date.prototype.getUTCDay()
Date.prototype.getUTCFullYear()
Date.prototype.getUTCHours()
Date.prototype.getUTCMilliseconds()
Date.prototype.getUTCMinutes()
Date.prototype.getUTCMonth()
Date.prototype.getUTCSeconds()
Date.prototype.getYear()
Date.prototype.setDate()
Date.prototype.setFullYear()
Date.prototype.setHours()
Date.prototype.setMilliseconds()
Date.prototype.setMinutes()
Date.prototype.setMonth()
Date.prototype.setSeconds()
Date.prototype.setTime()
Date.prototype.setUTCDate()
Date.prototype.setUTCFullYear()
Date.prototype.setUTCHours()
Date.prototype.setUTCMilliseconds()
Date.prototype.setUTCMinutes()
Date.prototype.setUTCMonth()
Date.prototype.setUTCSeconds()
Date.prototype.setYear()
Date.prototype.toDateString()
Date.prototype.toGMTString()
Date.prototype.toISOString()
Date.prototype.toJSON()
Date.prototype.toLocaleDateString()
Date.prototype.toLocaleFormat()
Date.prototype.toLocaleString()
Date.prototype.toLocaleTimeString()
Date.prototype.toSource()
Date.prototype.toString()
Date.prototype.toTimeString()
Date.prototype.toUTCString()
Date.prototype.valueOf()
Date.prototype[@@toPrimitive]
一元加法 | 加法
+new Date()
// 1546335448125
0+new Date()
// “0Tue Jan 01 2019 17:53:17 GMT+0800 (中国标准时间)”
二元加法获取 date 的值;
加法运算
上图中的 NOTE 1 说,在步骤 5 和 6 中的 ToPrimitive 调用没有提供 hint,除了 Date 对象之外所有 ECMAScript 对象将缺少 hint 的情况当做 Number 处理;
js 各个类型的 toPrimitive
而本文需要看的是 date[@@toPrimitive]() 方法可以转换一个 Date 对象到一个原始值。
Date()[Symbol.toPrimitive](hint)
==>date.toPrimitive;
给出的 Date 的原始值。根据传入参数的不同,可以返回 string 或 number 类型。
Date 对象的 [@@toPrimitive]() 方法可以返回一个原始值,或是 string,或是 number。
如果 hint 是 “string” 或 “default”,[@@toPrimitive]() 将会调用 toString。如果 toString 属性不存在,则调用 valueOf。如果 valueOf 也不存在,则抛出一个 TypeError。
如果 hint 是 “number”,[@@toPrimitive]() 会首先尝试 valueOf,若失败再尝试 toString。
当期望一个原始值却收到一个对象时,JavaScript 可以自动的调用 [@@toPrimitive]() 方法来将一个对象转化成原始值,所以你很少会需要自己调用这个方法。
date 的 toPrimitive 调用,不传递 hint 的时候,默认是 string;
所以:
0+new Date()
// “0Tue Jan 01 2019 17:53:17 GMT+0800 (中国标准时间)”

一元加法获取 date 的值
‘+’ 号运算符作为一元运算符时,Expression 将进行 ToNumber 操作。

可以看到,此时 date 的 toPrimitive 调用,传递 hint 是 number,返回毫秒数;
参考:JavaScript 加号运算符详解

正文完
 0