方法如下:
function diffDate(sealStartDate, sealEndDate) {

const startDate = moment(sealStartDate);
const endDate = moment(sealEndDate);
const duration = moment.duration(endDate.diff(startDate));
const years = duration.get('years');
const months = duration.get('months');
const days = duration.get('days');
let str = ''
if (years > 0 && months > 0 && days > 0) {
  str = years + '年' + months + '月' + days + '日'
} else if (years > 0 && months > 0) {
  str = years + '年' + months + '月'
} else if (years > 0 && days > 0) {
  str = years + '年' + days + '日'
} else if (months > 0 && days > 0) {
  str = months + '月' + days + '日'
} else if (years > 0) {
  str = years + '年'
} else if (months > 0) {
  str = months + '月'
} else if (days > 0) {
  str = days + '日'
} else {
  str = '0'
}
return str

}
最常见使用方法:
1、判断B日期是否在A日期前面:moment(B).isBefore(A)
2、日期格式化:moment().format(YYYY MM dd hh:mm:ss)
3、获取年份:moment().year() 或 moment().get('year')
4、获取月份:moment().month() 或 moment().get('month')


史晶晶
78 声望3 粉丝

菜鸟爱学习