momentjs 日期差异 return 错误

momentjs date difference return wrong

我使用 countdown.js 并想将 60 days 添加到 date(从数据库中获取的数据)。我在 targetDay 中做了这个并且工作正常,现在我想从现在开始计算这个日期但是它 return 一个奇怪的日期 "1969-11-05T21:24:07.416Z" 为什么?

const nowDate = moment();
const targetDay = moment('2020-10-24 14:25:26').add('60', 'days');
const countdown = moment(nowDate - targetDay);


console.log(countdown);
//const diff = targetDay.fromNow();

const count_days = countdown.format('D');
const count_hours = countdown.format('HH');
const count_minutes = countdown.format('mm');
const count_seconds = countdown.format('ss');
console.log(count_days + ' days:' + count_hours + ' hrs:' + count_minutes + ' m:' + count_seconds + ' s');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

我尝试了 fromNow() 功能,但它 return 在 string 中。我想做的是,从现在开始为目标日期倒计时。

使用moment.diff()代替方向数学减法。

这将创建一个 moment.duration(默认以毫秒为单位)。

const nowDate = moment();
const targetDay = moment('2020-10-24 14:25:26').add('60', 'days');
const countdownDiff = targetDay.diff(nowDate)
// 4855463420

你的差异常数也应该用持续时间来衡量,因为添加差异的格式不是持续时间

eg. 28 October + 60 days => 28 December. But moment('28-12-2020').format('D') is 28 isntead of the 60 you want to display.

因此您也需要将它们单独存储以作为区别

const count_days = targetDay.diff(nowDate, 'd');
const count_hours = targetDay.diff(nowDate, 'h');
const count_minutes = targetDay.diff(nowDate, 'm');
const count_seconds = targetDay.diff(nowDate, 's');

如果你想把它转换回一个瞬间,你可以在原来的基础上加上差异。这仅作为差异计算方式的说明

const countdown = nowDate.add(countdownDiff, 'ms')
// obviously this is redundant as it's the same as targetDay

这是一个工作示例:

const nowDate = moment();
const targetDay = moment('2020-10-24 14:25:26').add('60', 'days');
const countdownDiff = targetDay.diff(nowDate)
// 4855463420


const countdown = moment(nowDate).add(countdownDiff, 'ms')
// obviously this is redundant as it's the same as targetDay

const count_days = targetDay.diff(nowDate, 'days');
const count_hours = targetDay.diff(nowDate, 'h');
const count_minutes = targetDay.diff(nowDate, 'm');
const count_seconds = targetDay.diff(nowDate, 's');

console.log('Current day:', nowDate.format('Do MMM YYYY HH:mm'));
console.log('Target day:', targetDay.format('Do MMM YYYY HH:mm'));
console.log('Countdown end date', countdown.format('Do MMM YYYY HH:mm'));

console.log(count_days + ' days:' + count_hours + ' hrs:' + count_minutes + ' m:' + count_seconds + ' s');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

你可以用.duration()然后用.hours().minutes().seconds()得到你的剩余时间,这些函数从milliseconds[=19=转换而来]

https://momentjs.com/docs/#/durations/

setInterval(function() { // remove
  const nowDate = moment();
  const targetDay = moment('2030-10-24 14:25:26').add('60', 'days'); // I changed to 2030 to keep this snippet live for future ;)
  const countdown = moment.duration(targetDay.diff(nowDate));

  const count_days = Math.floor(countdown.asDays());
  const count_hours = countdown.hours();
  const count_minutes = countdown.minutes();
  const count_seconds = countdown.seconds();
  $('div').text(count_days + ' days:' + count_hours + ' hrs:' + count_minutes + ' m:' + count_seconds + ' s');
}, 1000); // remove
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<div></div>

只需删除代码中的 setInterval,这仅用于 演示!


我更改为 2030 年以保持此代码段在未来有效 ;)