Meet the deceased again: JavaScript Date

Cause

The reason why I want to record JavaScript Date is because I have recently discovered several new uses of Date, which I haven't noticed before, so record it.

Print the value of the Date instance

new Date()					// Tue Jun 16 2020 00:47:33 GMT+0800 (中国标准时间)
new Date().toString()		// "Tue Jun 16 2020 00:47:59 GMT+0800 (中国标准时间)"
new Date().toUTCString()	// "Mon, 15 Jun 2020 16:47:59 GMT"
new Date().toISOString() 	// GMTString | LocaleString ...
new Date().valueOf()		// 1592239679948
new Date().getTime()		// 1592239679948
+new Date()					// 1592239679948

Date instance addition and subtraction

+date				// 1592239679948	# Tue Jun 16 2020 00:47:59 GMT+0800 (中国标准时间)
+secondDate			// 1592240090940	# Tue Jun 16 2020 00:54:50 GMT+0800 (中国标准时间)

secondDate - date	// 410992			# 410992 / 1000 / 60 ≈ 6.85 m

secondDate + date	// "Tue Jun 16 2020 00:54:50 GMT+0800 (中国标准时间)Tue Jun 16 2020 00:47:59 GMT+0800 (中国标准时间)"
+secondDate + date	// "1592240090940Tue Jun 16 2020 00:47:59 GMT+0800 (中国标准时间)"
+secondDate + +date	// 3184479770888	# Sat Nov 29 2070 17:42:50 GMT+0800 (中国标准时间)

new Date(+date + 1000 * 60 * 7)			// Tue Jun 16 2020 00:54:59 GMT+0800 (中国标准时间)	# date 7 min 后
new Date(+date + 1000 * 60 * 60 * 24)	// Wed Jun 17 2020 00:47:59 GMT+0800 (中国标准时间)	# date 1天后

Date Automatically convert year, month and day

// 6月共30天
const Jun30 = new Date("2020-06-30")			// Tue Jun 30 2020 08:00:00 GMT+0800 (中国标准时间)

new Date(+Jun30 + 1000 * 60 * 60 * 24)			// Wed Jul 01 2020 08:00:00 GMT+0800 (中国标准时间)
new Date(+Jun30 + 1000 * 60 * 60 * 24 * 32)		// Sat Aug 01 2020 08:00:00 GMT+0800 (中国标准时间)
new Date(+Jun30 + 1000 * 60 * 60 * 24 * 365)	// Wed Jun 30 2021 08:00:00 GMT+0800 (中国标准时间)

// 2020年2月共29天
const Feb28 = new Date("2020-02-28")			// Fri Feb 28 2020 08:00:00 GMT+0800 (中国标准时间)

new Date(+Feb28 + 1000 * 60 * 60 * 24)			// Sat Feb 29 2020 08:00:00 GMT+0800 (中国标准时间)	# 自动计算闰年
new Date(+Feb28 + 1000 * 60 * 60 * 24 * 366)	// Sun Feb 28 2021 08:00:00 GMT+0800 (中国标准时间)

to sum up

  1. +new Date() Equivalent to new Date().getTime()
  2. Date instances can be added and subtracted
  3. Date automatically converts year, month and day (including leap year and February)

Guess you like

Origin blog.csdn.net/qq_37164975/article/details/106774891
Recommended