简体   繁体   English

使用Moment.js以分钟格式比较时间,该格式不会在小时上重置

[英]Comparing time using Moment.js in a minute format that doesn't reset on the hour

I am trying to create a count up timer based on an object's modified time (formatted like: 2014-02-19T18:49:15) and comparing it to the current time using Moment.js. 我试图根据对象的修改时间(格式为:2014-02-19T18:49:15)创建一个递增计时器,并使用Moment.js将其与当前时间进行比较。 But when it reaches 60 minutes, it restarts back to 0. I think this is because I am comparing just the minutes here or at least using math to change the milliseconds to a whole number format? 但是,当达到60分钟时,它会重新恢复为0。我认为这是因为我只是在这里比较分钟,还是至少使用数学将毫秒更改为整数格式? I am not quite sure, I got excited when I got the minutes to format correctly. 我不太确定,当我有时间进行正确格式化时感到很兴奋。 The difference in minutes is all I want to return. 分钟差就是我要返回的全部。 So for example after an hour and a half I want a returned value of '90'. 因此,例如,一个半小时后,我希望返回值为“ 90”。

function() {
    return function(entry) {
        var elpTime = Math.floor(((Date.parse(moment()) - Date.parse(entry.arrival_time)) / (1000*60)) % 60);

        return elpTime;
    }
};

Here is an example of the object. 这是对象的示例。

{
patient_id: 198,
arrival_time: "2014-02-19T18:49:15",
last_modified: "2014-02-19T18:49:15"
}

I know I am missing something probably obvious. 我知道我想念的东西可能很明显。 But any help is appreciated. 但是任何帮助都值得赞赏。 Thanks! 谢谢!

(Should be noted that I am using this as apart of a filter function in Angular.js. But I stripped it out since I didn't think it was nessacary) (应该注意的是,我在Angular.js中将此功能用作过滤器功能的一部分。但是我将其删除,因为我认为这不是必要的)

Since you are using Moment.js you can use the built in diff function to return the time period between two moments. 由于您正在使用Moment.js ,因此可以使用内置的diff函数来返回两个时刻之间的时间段。

function() {
    return function(entry) {
        var elpTime = moment(entry.arrival_time).diff(moment(),'minutes');

        return elpTime;
    }
};

From the documentation: 从文档中:

The supported measurements are years, months, weeks, days, hours, minutes, and seconds. 支持的度量是年,月,周,天,小时,分钟和秒。

By default, moment#diff will return number rounded down. 默认情况下,moment#diff将返回四舍五入的数字。 If you want the floating point number, pass true as the third argument. 如果需要浮点数,则将true作为第三个参数传递。 Before 2.0.0, moment#diff returned rounded number, not a rounded down number. 在2.0.0之前,moment#diff返回的是四舍五入的数字,而不是四舍五入的数字。

Example from documentation: 文档中的示例:

var a = moment([2008, 6]);
var b = moment([2007, 0]);
a.diff(b, 'years');       // 1
a.diff(b, 'years', true); // 1.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM