Javascript 如何用moment.js减去2次,然后减去一些分钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34247283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:05:24  来源:igfitidea点击:

How to subtract 2 times with moment.js, then subtract some minutes

javascriptmomentjs

提问by HerrimanCoder

I need to subtract 2 times with moment.js (get the difference), and then with that result, subtract some additional minutes (simple int). It's for calculating timesheets. A few examples:

我需要用 moment.js 减去 2 次(得到差值),然后用这个结果减去一些额外的分钟(简单整数)。它用于计算时间表。几个例子:

Example #1:
Start time: 10:00 AM (represented in js as "10:00")
End time: 2:00 PM (represented in js as "14:00")
Lunch: 30 minutes ("30")
Expected result: "3:30" (10am - 2pm is 4 hours, minus 30 minutes for lunch = 3hrs 30 mins -- and I need it output as "3:30")

Example #2:
Start time: 6:15 AM (represented in js as "6:15")
End time: 4:45 PM (represented in js as "16:45")
Lunch: 0 minutes ("0")
Expected result: "10:30"

I know moment.js can do this but I'm struggling to get expected results. I've been trying this:

我知道 moment.js 可以做到这一点,但我正在努力获得预期的结果。我一直在尝试这个:

function getTimeInterval(startTime, endTime){
        return moment(moment(startTime,"hh:mm").diff(moment(endTime,"hh:mm"))).format("hh:mm"); 
}

The formatting seems right, but I'm getting incorrect values. For example, the result returned for my example #2 is "6:30" instead of "10:30" And then how do I subtract off int minutes for lunch?

格式似乎正确,但我得到的值不正确。例如,我的示例#2 返回的结果是“6:30”而不是“10:30”然后我如何减去午餐的 int 分钟?

Any help is much appreciated.

任何帮助深表感谢。

回答by Matt Johnson-Pint

// parse time using 24-hour clock and use UTC to prevent DST issues
var start = moment.utc(startTime, "HH:mm");
var end = moment.utc(endTime, "HH:mm");

// account for crossing over to midnight the next day
if (end.isBefore(start)) end.add(1, 'day');

// calculate the duration
var d = moment.duration(end.diff(start));

// subtract the lunch break
d.subtract(30, 'minutes');

// format a string result
var s = moment.utc(+d).format('H:mm');

Pay close attention to the casing of the formats. You were using hhwhich is for a 12-hour clock.

密切注意格式的外壳。您使用的hh是 12 小时制。

See also: Get the time difference between two datetimes

另请参阅: 获取两个日期时间之间的时差

回答by VincenzoC

You can use the diffmethod to calculate the difference between two dates and the subtractmethod to subtract time. In your case:

您可以使用该diff方法来计算两个日期之间的差异和subtract减去时间的方法。在你的情况下:

function getTimeInterval(startTime, endTime, lunchTime){
    var start = moment(startTime, "HH:mm");
    var end = moment(endTime, "HH:mm");
    var minutes = end.diff(start, 'minutes');
    var interval = moment().hour(0).minute(minutes);
    interval.subtract(lunchTime, 'minutes');
    return interval.format("HH:mm");
}