javascript字符串转日期类型

Date 的构造方法

new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
  • 无参数 : 取当前时间。
  • value : 相对于 “January 1, 1970, 00:00:00 UTC” 的毫秒。
  • dateString : 符合 IETF-compliant RFC 2822 timestamps 或 ISO 8601 Extended Format(es5开始支持),以及非标格式
  • year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]] : 年月日时分秒

ISO 8601 Extended Format 示例

chrome console> console.log(new Date('2019-12-11T08:30:05+08:00'));
Wed Dec 11 2019 08:30:05 GMT+0800 (中国标准时间)
chrome console> console.log(new Date('2019-12-11T08:30:05'));
Wed Dec 11 2019 08:30:05 GMT+0800 (中国标准时间)
chrome console> console.log(new Date('2019-12-11T08:30:05+00:00'));
Wed Dec 11 2019 16:30:05 GMT+0800 (中国标准时间)
chrome console> console.log(new Date('2019-12-11T08:30:05Z'));
Wed Dec 11 2019 16:30:05 GMT+0800 (中国标准时间)

浏览器兼容

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Browser_compatibility

参考

https://blog.csdn.net/beta_xiyan/article/details/76991635
https://segmentfault.com/a/1190000017579541
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

IETF-compliant RFC 2822 timestamps

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.

形如:

Sun 30 Dec 2018 19:42:44 +0800

ISO 8601 Extended 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.

精简一下:

ISO 8601 Extended Format : YYYY-MM-DDTHH:mm:ss.sssZ

示例:

1995-12-17T03:24:00+00:00 //标准时间
1995-12-17T03:24:00Z //标准时间
1995-12-17T03:24:00+08:00 //东8区时间 
发布了284 篇原创文章 · 获赞 54 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/sayyy/article/details/103496774
今日推荐