从X开始的时间没有像预期的那样工作

Time from X not working as expected moment.js

本文关键字:工作 开始 时间      更新时间:2023-09-26
 $.each(data[i].replies, function(m, n) {
var currentdate = new Date();

                    console.log(n.entry.date_entered);
                    check = moment(n.entry.date_entered, 'YYYY/MM/DD');
                    check1 = moment(currentdate, 'YYYY/MM/DD');
                     console.log(check);
                     console.log(check1);
                    var month = check.format('M');
                    var day = check.format('DD');
                    var year = check.format('YYYY');
                     var month1 = check1.format('M');
                    var day1 = check1.format('DD');
                    var year1 = check1.format('YYYY');

                    get = moment([year, month, day]);
                     get1 = moment([year1, month1, day1]);
                   g = get1.from(get);
});

样本 n.entry.date_entered: 2014-07-28 12:23:43

对于所有的日期我得到几秒钟前不知道为什么

我认为你的问题是你传递给moment的格式掩码。

在示例中使用-作为分隔符,但在格式掩码中使用/。这样moment将无法解析日期,而会给你当前日期。

尝试将格式掩码更改为"YYYY-MM-DD"