momentJS时间差计算
最近在使用JavaScript计算时间差的时候,发现很多问题需要处理,在查看momentJS之后,发现非常容易。
const dateFormat = "YYYY-MM-DD HH:mm:ss"
moment().format(dateFormat) //当前时间 2020-12-30 17:08:42
moment().endOf('day').format(dateFormat); //当天时间 2021-12-30 23:59:59
moment().startOf('year').format(dateFormat); // 当年时间 2021-01-01 00:00:00
moment().startOf('week').format(dateFormat); // 获取本周周日 0时0分0秒
moment().startOf('isoWeek').format(dateFormat); // 获取本周周一 0时0分0秒
moment().subtract(10, "days").format(dateFormat); //当前时间的前10天时间
moment().subtract(1, "years").format(dateFormat); //当前时间的前1年时间
moment().subtract(3, "months").format(dateFormat); //当前时间的前3个月时间
moment().subtract(1, "weeks").format(dateFormat); //当前时间的前一个星期时间
计算两个时间的差值
var m1 = moment('2018-08-14 11:00:00'),
m2 = moment('2018-08-14 12:10:00');
console.log(m1)
console.log(m2)
console.log(m2.diff(m1, 'minute'));
minute为分钟,可以更改为天年秒..
其他一些方法请访问网址 www.cnblogs.com/Renyi-Fan/p…