【问题标题】:momentjs date difference return wrongmomentjs 日期差返回错误
【发布时间】:2020-10-28 08:30:10
【问题描述】:

我使用countdown.js 并想将60 days 添加到date(数据取自数据库)。我在targetDay 中执行此操作,它工作正常,现在我想从现在开始计算这个日期,但它返回一个奇怪的日期"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() 函数,但它返回string。我想做的是,从现在开始对目标日期进行倒计时。

【问题讨论】:

  • nowDate - targetDay 等于-4855971015。将其传递给时刻可以追溯到 1970 年 1 月 1 日的约 56 天
  • moment() 用于日期。 moment.duration() 是持续时间。您需要调整获取和使用countdown 的方式。 const countdown = moment.duration(nowDate.diff(targetDay));
  • @Ouroborus 你试过这个吗?不适合我!
  • 当然,您需要进行进一步的调整,因为.format() 之类的内容不适用于持续时间。阅读the documentation 可能会有所帮助。
  • 考虑到我的第一条评论正是在回答您提出的问题,这一切都很有趣:it return an odd date "1969-11-05T21:24:07.416Z" why? 无论如何,我不在乎代表,我也不是来贬低新手的.无论如何你都不是一个新手,你最早的 JS 问题是从大约三年前开始的。

标签: javascript momentjs


【解决方案1】:

您可以使用.duration(),然后使用.hours().minutes().seconds() 获取剩余时间,这些函数从milliseconds 转换而来

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 年是为了让这个 sn-p 在未来继续存在;)

【讨论】:

    【解决方案2】:

    使用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
    

    您的差异常数也应该以持续时间来衡量,因为附加差异的格式不是持续时间

    例如。 10 月 28 日 + 60 天 => 12 月 28 日。但是moment('28-12-2020').format('D')28 而不是您要显示的60

    因此,您还需要将它们单独存储为差异

    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>

    【讨论】:

    • @ChrisG 我对问题/答案持怀疑态度。虽然我自己鄙视帮助吸血鬼,但我很难判断 OP 是否真的只是不了解持续时间和日期之间的区别,还是真的只是在寻找某人为他们做这项工作。就我个人而言,我不介意将它删除为 cmets 中显示的行为,但同时我觉得答案对于其他在实际访问问题后几天查看答案的用户群实际上可能有用。也许值得特别打开关于用户的案例@meta?
    • 我已经删除了我的 cmets;我同意这个答案可能对其他人有用。在我看来,进一步追求这一点是不值得的。根据我的经验,最好坚持反对票和关闭票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多