且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数天直到今天moment.js

更新时间:2023-01-31 14:36:07

如果您遇到的问题是使用 moment.js 获取两个日期之间的持续时间,然后您可以使用 diff 这样的函数:

If the problem you have is to use moment.js to get the duration between two dates, then you can use the diff function like this:

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
var diffInMs = a.diff(b); // 86400000 milliseconds
var diffInDays = a.diff(b, 'days'); // 1 day

现在,我不知道你是否对KnockoutJS有任何问题,但是这个应该确保你的计算是用 moment.js 完成的。

Now, I don't know if you have any problem with KnockoutJS, but this should ensure that your computation is done with moment.js.

为了你的兴趣,我自己做了一个自定义绑定处理程序来显示片刻日期前一段时间。与你的不同之处在于我的观察力已经是一个时刻的对象。所以,我在这里对其进行了修改以使其适用于标准日期对象:

Just for your interest, I made myself a custom binding handler for displaying a moment date some time ago. The difference with yours is that my observable was already a moment object. So, I've modified it down here to make it work with standard date objects:

    ko.bindingHandlers.moment = {
        update: function(element, valueAccessor) {
            var value = valueAccessor();
            var formattedValue = moment(ko.utils.unwrapObservable(value)).format('MM/DD/YYYY');
            $(element).text(formattedValue);
        }
    };

编辑:我给你一个小提示示例。

I made you a fiddle with the example.