• js是毫秒级时间戳
  • 在js的世界, 一天=86,400,000

data构造两种比较方便的做法:

//方法一, 比较讨厌的是月份值从0-11.
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
//iso8601格式: YYYY-MM-DDTHH:mm:ss.sssZ
new Date(2018-1-25)
//这其中T是分割用的, Z代表utc, 可以是+-HH:mm代表时区

得到时间戳

Date.parse(dateString) //显示调用
new Date(dateString).getTime() //隐式调用了上面
new Date(dateString).valueOf() //等于上面那个
Date.now(); //拿到当前时间点的时间戳

使用时间戳

dateObj.setTime(timeValue)
new Date(timeValue);

isostring

date.toISOString()
date.toJSON().slice(0,20)
//这个很有用, 按照当前时区和语言显示
new Date(timeValue).toLocaleDateString() 

得到年月日星

年: dateObj.getFullYear()
月: dateObj.getMonth()  0-11
日: dateObj.getDate()
星: dateObj.getDay()   0-6

年月日 和 年月

//两个等价的字符串
new Date().toISOString() 
//"2020-08-09T10:48:49.550Z"

new Date().toJSON()
//"2020-08-09T10:49:10.969Z"

new Date().toJSON().slice(0,20)

//年月日
new Date().toJSON().slice(0,10) //这个并不好, 这里拿到的是utc0的时间, 格兰威治时间.
''+t.getFullYear()+(t.getMonth()+1)+t.getDate()

//年月
new Date().toJSON().slice(0,7)

//- 减号不是合法的key值, 因此要处理下.
new Date().toJSON().slice(0,10).replace(/-/g,'')


昨天, 今天, 明天


const tomorrow= new Date(Date.now() + 24 * 60 * 60 * 1000);
const yesterday= new Date(Date.now() - 24 * 60 * 60 * 1000);

//本月
new Date().getMonth() + 1
new Date().toJSON().slice(0,7)

//下个月
 var m=new Date()
 m.setMonth(m.getMonth()+1)
m.toJSON().slice(0,7)
//上个月
 m.setMonth(m.getMonth()-1)
m.toJSON().slice(0,7)

//月的第一天
m.setDate(1)

参考: https://segmentfault.com/a/1190000015718238