moment笔记

moment计算时间

1、moment加法

console.log(
 moment(new Date()).add(1, 'days').format('YYYY-MM-DD')
) // 2019-10-26 ; 在今天的基础上加了一天
console.log(
 moment(new Date()).add(1, 'months').format('YYYY-MM-DD')
) //2019-11-25 ; 在本月的基础上加了一天
console.log(
 moment(new Date()).add(1, 'years').format('YYYY-MM-DD')
) // 2020-10-25 ; 在今年的基础上加了一天

2、moment减法

console.log(
 moment(new Date()).subtract(1, 'days').format('YYYY-MM-DD')
) // 2019-10-24 ; 在今天的基础上减了一天
console.log(
 moment(new Date()).subtract(1, 'months').format('YYYY-MM-DD')
) //2019-09-25 ; 在本月的基础上减了一天
console.log(
 moment(new Date()).subtract(1, 'years').format('YYYY-MM-DD')
)

3、moment计算两个时间点的时间差

const start = moment(moment(self.props.form.getFieldValue('rentTime')).format('YYYY-MM-DD'))
const end = moment(moment(dateString).add(1, 'days').format('YYYY-MM-DD'))
self.props.form.setFieldsValue({
 rentLife: end.diff(start, 'day')
}); // self.props.form.getFieldValue 和 self.props.form.setFieldsValue 是 antd form 的 api

3、moment计算两个时间点先后顺序

const currentDate = new Date()
const start = moment(self.props.form.getFieldValue('rentTime')).format('YYYY-MM-DD')
return currentDate.isBefore(moment(start).add(0, 'days'))
                        

const currentDate = new Date()
const start = moment(self.props.form.getFieldValue('rentTime')).format('YYYY-MM-DD')
return currentDate.isAfter(moment(start).add(0, 'days'))

原文地址:https://www.cnblogs.com/lan1974/p/11868139.html