使用moment.js计算两个时间的差值,返回年/月/日/时/分/秒

484 阅读1分钟
  • 官网地址:momentjs.cn/docs/#/dura…
  • 语法:let duration = moment.duration(x.diff(y))
  • 返回时长对象,其时长在 x 和 y 之间。
 let m1 = moment("2020-11-11 11:15:00"),
     m2 = moment("2020-11-11 11:48:00");
    let duration = moment.duration(m2.diff(m1));
    let { _data } = duration;
    // console.log(duration);
    // console.log(
    //   "年:" + _data.years,
    //   "月:" + _data.months,
    //   "日:" + _data.days,
    //   "时:" + _data.hours,
    //   "分:" + _data.minutes
    // );
    //返回年:0 月:0 日:0 时:0 分:33

出来的数据 在这里插入图片描述