关于时间的操作,一直在使用momentjs这个库。方便灵巧,功能强大。唯一的缺点:包大小为200K。精简的方案:如dayjs和miment。
dayjs本身就是对标momentjs进行开发的:
Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for modern browsers with a largely Moment.js-compatible API. If you use Moment.js, you already know how to use Day.js.
它的用法非常简单。
dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss');
和momentjs用法基本一模一样的。dayjs的API和moment几乎一模一样,所以如果想要替换到现有的momentjs代码,直接替换为dayjs即可。而且 dayjs的大小: 2KB!!!
安装
NPM:
npm install dayjs --save
import dayjs from 'dayjs'
// 或者 CommonJS
// var dayjs = require('dayjs');
dayjs().format();
CDN:
<!-- 最新的压缩后的 JavaScript 文件 -->
<script src="https://unpkg.com/dayjs"></script>
<script>
dayjs().format();
</script>
常用API参考:
- 当前时间
dayjs()- 时间字符串
dayjs(‘2018-06-03’)- 时间戳
dayjs(1528361259484)- Date 对象
dayjs(new Date(2018,8,18))- 复制
dayjs().clone()- 检测当前 Dayjs 对象是否是一个有效的时间
dayjs().isValid()取值赋值
获取/设置
年 : dayjs().year()月 : dayjs().month()
季度 : dayjs().quarter()
日 : dayjs().date()
星期 : dayjs().day()
时 : dayjs().hour()
分 : dayjs().minute()
秒 : dayjs().second()
毫秒 : dayjs().millisecond()获取
dayjs().get(‘year’)
dayjs().get(‘month’)设置
dayjs().set(‘year’,2017)
dayjs().set(‘month’,9)所有可用单位列表
单位 缩写 描述 date D 日期 day d 星期(星期日0,星期六6) month M 月份(0-11) year y 年 hour h 小时 minute m 分钟 second s 秒 millisecond ms 毫秒 操作
增加时间并返回一个新的 Dayjs() 对象
dayjs().add(7, ‘day’)
dayjs().add(7, ‘year’)所有可用单位列表
单位 缩写 描述 week w 周 day d 星期(星期日0,星期六6) month M 月份(0-11) quarter Q 依赖 QuarterOfYear
插件year y 年 hour h 小时 minute m 分钟 second s 秒 millisecond ms 毫秒 减少时间并返回一个新的 Dayjs() 对象
dayjs().subtract(7, ‘year’)
dayjs().subtract(7, ‘month’)返回当前时间的开头时间的 Dayjs() 对象,如月份的第一天。
dayjs().startOf(‘year’)
dayjs().startOf(‘month’)所有可用单位列表
单位 缩写 描述 date D 天00:00 day d 星期00:00 month M 月第一天00:00 quarter Q 季度第一个月第一天00:00,依赖 QuarterOfYear
插件year y 1月1日00点 week w 周第一天00:00 isoWeek 周(ISO) hour h 00:00:00 minute m 00:00 second s 00 millisecond ms 0 返回当前时间的末尾时间的 Dayjs() 对象,如月份的最后一天。
dayjs().endOf(‘month’)
dayjs().endOf(‘year’)显示
格式化
dayjs().format()
dayjs().format(‘YYYY-MM-DD dddd HH:mm:ss.SSS A’)
Format Output Description YY 18 年,两位数 YYYY 2018 年,四位数 M 1-12 月,从1开始 MM 01-12 月,两位数字 MMM Jan-Dec 月,英文缩写 D 1-31 日 DD 01-31 日,两位数 H 0-23 24小时 HH 00-23 24小时,两位数 h 1-12 12小时 hh 01-12 12小时,两位数 m 0-59 分钟 mm 00-59 分钟,两位数 s 0-59 秒 ss 00-59 秒,两位数 S 0-9 毫秒(百),一位数 SS 00-99 毫秒(十),两位数 SSS 000-999 毫秒,三位数 Z -05:00 UTC偏移 ZZ -0500 UTC偏移,两位数 A AM / PM 上/下午,大写 a am / pm 上/下午,小写 Do 1st… 31st 月份的日期与序号 时间差
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’)
单位 缩写 描述 week w 周 day d 星期(星期日0,星期六6) month M 月份(0-11) quarter Q 依赖 QuarterOfYear
插件year y 年 hour h 小时 minute m 分钟 second s 秒 millisecond ms 毫秒 Unix 时间戳 (毫秒)
dayjs().valueOf()Unix 时间戳 (秒)
dayjs().unix()返回月份的天数
dayjs().daysInMonth()获取月天数
dayjs().toDate()
dayjs('2019-01-25').daysInMonth() // 31
返回原生的 Date 对象
dayjs().toDate()返回包含时间数值的数组
dayjs().toArray()dayjs('2019-01-25').toArray() // [ 2019, 0, 25, 0, 0, 0, 0 ]
当序列化 Dayjs 对象时,会返回 ISO8601 格式的字符串
dayjs().toJSON() //2018-06-08T02:44:30.599Z返回 ISO8601 格式的字符串
dayjs().toISOString() //2018-06-08T02:46:06.554Z返回包含时间数值的对象
dayjs().toObject()dayjs('2019-01-25').toObject() /* { years: 2019, months: 0, date: 25, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 } */
字符串
dayjs().toString()查询
- 检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之前
dayjs(‘2018-06-01’).isBefore(dayjs(‘2018-06-02’))- 检查一个 Dayjs 对象是否和另一个 Dayjs 对象时间相同
dayjs().isSame(dayjs())- 检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之后
dayjs().isAfter(dayjs())