Day.js :一个轻量的处理时间和日期的 JavaScript 库
https://blog.csdn.net/Wave_explosion/article/details/118613794?spm=1001.2014.3001.5501
后附 官网 文档
安装:
npm install dayjs --save
(童哥这里用的npm
)
CDN:
<script src="https://unpkg.com/dayjs"></script>
<script>
dayjs().format();
</script>
文档获取看哈就行,要啥看啥:
当前时间
dayjs()
时间字符串
dayjs('2018-06-03')
时间戳
dayjs(1528361259484)
Date 对象
dayjs(new Date(2018,8,18))
复制
dayjs().clone()
检测当前 Dayjs 对象是否是一个有效的时间
dayjs().isValid()
获取
年 : dayjs().year()
月 : dayjs().month()
日 : dayjs().date()
星期 : dayjs().day()
时 : dayjs().hour()
分 : dayjs().minute()
秒 : dayjs().second()
毫秒 : dayjs().millisecond()
设置
dayjs().set('year',2017)
dayjs().set('month',9)
增加时间并返回一个新的 Dayjs() 对象
dayjs().add(7, 'day')
dayjs().add(7, 'year')
减少时间并返回一个新的 Dayjs() 对象
dayjs().subtract(7, 'year')
dayjs().subtract(7, 'month')
返回当前时间的开头时间的 Dayjs() 对象,如月份的第一天。
dayjs().startOf('year')
dayjs().startOf('month')
返回当前时间的末尾时间的 Dayjs() 对象,如月份的最后一天。
dayjs().endOf('month')
dayjs().endOf('year')
格式化
dayjs().format()
dayjs().format('YYYY-MM-DD dddd HH:mm:ss.SSS A')
时间差
dayjs('2018-06-08').diff(dayjs('2017-06-01'),'years')
dayjs('2018-06-08').diff(dayjs('2017-06-01'),'day')
dayjs('2018-06-08').diff(dayjs('2017-06-01'),'hour')
Unix 时间戳 (毫秒)
dayjs().valueOf()
Unix 时间戳 (秒)
dayjs().unix()
返回月份的天数
dayjs().daysInMonth()
返回原生的 Date 对象
dayjs().toDate()
返回包含时间数值的数组
dayjs().toArray()
当序列化 Dayjs 对象时,会返回 ISO8601 格式的字符串
dayjs().toJSON() //2018-06-08T02:44:30.599Z
返回 ISO8601 格式的字符串
dayjs().toISOString() //2018-06-08T02:46:06.554Z
返回包含时间数值的对象
dayjs().toObject()
字符串
dayjs().toString()
检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之前
dayjs('2018-06-01').isBefore(dayjs('2018-06-02'))
检查一个 Dayjs 对象是否和另一个 Dayjs 对象时间相同
dayjs().isSame(dayjs())
检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之后
dayjs().isAfter(dayjs())
获取两个 Dayjs 对象的时间差,默认毫秒。
const date1 = dayjs('2019-01-25')
const date2 = dayjs('2018-06-05')
date1.diff(date2) // 20214000000
date1.diff(date2, 'month') // 7
date1.diff(date2, 'month', true) // 7.645161290322581
date1.diff(date2, 'day') // 233
返回 Unix 时间戳 (毫秒)
dayjs().valueOf()
返回 Unix 时间戳 (秒)。
dayjs().unix()
返回月份的天数
dayjs().daysInMonth()
返回原生的 Date 对象。
dayjs().toDate()
当序列化 Dayjs 对象时,会返回 ISO8601 格式的字符串。
dayjs().toJSON() //"2018-08-08T00:00:00.000Z"
返回 ISO8601 格式的字符串。
dayjs().toISOString()
返回字符串
dayjs().toString()
单位 缩写 描述
date 日期
day d 星期几 (星期天 0, 星期六 6)
month M 月 (一月 0, 十二月 11)
year y 年
hour h 时
minute m 分
second s 秒
millisecond ms 毫秒
检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之前。
dayjs().isBefore(Dayjs, unit? : String);
dayjs().isBefore(dayjs()); // false
dayjs().isBefore(dayjs(), 'year'); // false
检查一个 Dayjs 对象是否和另一个 Dayjs 对象时间相同。
dayjs().isSame(Dayjs, unit? : String);
dayjs().isSame(dayjs()); // true
dayjs().isSame(dayjs(), 'year'); // true
检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之后。
dayjs().isAfter(Dayjs, unit? : String);
dayjs().isAfter(dayjs()); // false
dayjs().isAfter(dayjs(), 'year'); // false
此乃文档、官网收藏吧
https://blog.csdn.net/Wave_explosion/article/details/118613794?spm=1001.2014.3001.5501
![在这里插入图片描述]在这里插入图片描述)(不用了)
解构赋值:https://blog.csdn.net/Wave_explosion/article/details/121850521
官方:github https://github.com/iamkun/dayjs
中文使用文档: https://github.com/iamkun/dayjs/blob/master/docs/zh-cn/API-reference.md
cookei: https://blog.csdn.net/Wave_explosion/article/details/118613794