Moment.js是一个轻量级的js时间处理类库,其使用简单,方便了日常开发中对时间的操作,提高了开发效率。
npm install moment
moment()
moment(String)
moment().get('year')
moment().get('month')
moment().get('date')
.subtract(Number, String);
moment().subtract(1, 'years')
moment().subtract(1, 'months')
moment().subtract(1, 'days')
.startOf(String);
moment().startOf('day')
moment().startOf('week')
通过将原始的 moment 设置为时间单位的末尾来对其进行更改
.endOf(String);
moment().endOf('day')
moment().endOf('week')
.daysInMonth()
moment("2012-02", "YYYY-MM").daysInMonth() // 29
.unix() //秒数
.valueOf() //毫秒数
获取时间戳(以秒为单位)
moment().format('X').unix() // 返回值为数值型
获取时间戳(以毫秒为单位)
moment().format('x').valueOf() // 返回值为数值型
在ant的a-range-picker组件的disabledDate使用
不能选择今天之前的日期(包括今天)
disabledDate(current) {
return current && current < moment().endOf('day');
},
不能选择今天之前的日期(不包括今天)
disabledDate(current) {
return current && current < moment().subtract(1, 'days').endOf('day')
},
disabledDate(current) {
return current && current < moment('2019-01-01')
},