javascript parse date string - js 字符串转日期

一、日期数字

/*
 * 无论在哪台电脑上,同一时刻,下面的输出都是一样的:
 */
new Date().getTime() // 1665370859678

数字表示从 UTC+0 时区的1970年1月1日0时0分0秒开始的那一刻起,所经过的毫秒数。
无论是在北京还是伦敦,此时此刻,无论本地时间是几点,它们经过的毫秒数都是一样的。

二、日期字符串

日期字符串根数字不一样,字符串是被时区化了的结果。
比如: 2022-10-08 这个字符串,在中国和伦敦,所表示的经过的毫秒数的时间是不一样的。
也就是北京的本地时间2022年10月8日和伦敦的本地时间2022年10月8日是存在时区差的。

三、UTC 时间

UTC 相当于本初子午线(即经度0度)上的平均太阳时,过去曾用格林威治平均时(GMT)来表示。
GMT 格林尼治标准时间(Greenwich Mean Time)是指位于伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。
所以 UTC 时间也是有时区的,它的时区为 0 时区。


/*
 * 无论在哪台电脑上,同一时刻,下面的输出都是一样的:
 */
new Date().toUTCString()    

// 'Mon, 10 Oct 2022 03:45:19 GMT'


/*
 * 而 Date 对象的 toString() 方法,会根据所在电脑的时区,显示本地的时间。
 */
new Date().toString()    

// 'Mon Oct 10 2022 11:46:24 GMT+0800 (China Standard Time)'

四、字符串转日期

在将字符串转为 Date 对象时,需要指定一个时区,才能准确的表示时间。
(注:Date 对象也是一个没有时区限制的对象。只不过它有默认的本地时区,通常转换成字符时间表示时,默认自动转换成本地时区的时间。)
如果不指定时区, Date 对象会默认指定一个字符串的时区(根据字符串格式的不同)

When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as local time.

/*
 * 当:不指定时区解析字符串时的:默认时区
 */

// 以下:默认为 0 时区的日期
Date.parse("2011-10-10")   // date-only 只有日期

// 以下:默认为 本地时区的日期 
// 所以本地电脑时区为北京时间和伦敦时间时,解析出的日期是不一样的。
Date.parse("2011-10-10T14:48:00")   // date-time 日期+时间
Date.parse("10/10/2022")            // Non-standard date strings 非标准日期格式
Date.parse("October 10, 2014")      // Non-standard date strings 非标准日期格式
Date.parse("Oct 10, 2014")          // Non-standard date strings 非标准日期格式

五、解析字符串时如何指定时区

/*
 * 在字符串后面添加: GMT (或 UTC) 
 */ 

Date.parse('2011-10-10 GMT-0300')  // 西三区
Date.parse('2011-10-10 UTC-0300')  // 西三区

Date.parse('2011-10-10 UTC+0800')  // 东八区

Date.parse('2011-10-10 GMT')       // 0时区
Date.parse('2011-10-10 UTC')       // 0时区
Date.parse('2011-10-10 UTC+0000')  // 0时区

六、日期如何在数据库中存储

  • 可以使用数据库的 Date 类型,
  • 也可以使用 数字类型存储毫秒数。
  • 不建议使用字符串存储。尤其是跨时区的项目。

附:标准日期格式

21.4.1.18 Date Time String Format

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 calendar date extended format. 
The format is as follows: 

YYYY-MM-DDTHH:mm:ss.sssZ


Where the elements are as follows:

YYYY	is the year in the proleptic Gregorian calendar as four decimal digits from 0000 to 9999, or as an expanded year of "+" or "-" followed by six decimal digits.
-	"-" (hyphen) appears literally twice in the string.
MM	is the month of the year as two decimal digits from 01 (January) to 12 (December).
DD	is the day of the month as two decimal digits 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 UTC offset representation specified as "Z" (for UTC with no offset) or as either "+" or "-" followed by a time expression HH:mm (a subset of the time zone offset string format for indicating local time ahead of or behind UTC, respectively)


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 UTC offset representation appended:

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

转载请注明,原文出处:
https://www.cnblogs.com/eddyz/p/16775034.html

ref:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
https://tc39.es/ecma262/#sec-date-time-string-format

posted @ 2022-10-10 11:26  炎黄子孙,龙的传人  阅读(1918)  评论(0编辑  收藏  举报