简体   繁体   English

moment.js 中的时间表小时差

[英]Timesheet hours difference in moment.js

I have a string '08:30-16:30' describing working hours.我有一个字符串 '08:30-16:30' 描述工作时间。 I also have the time someone has worked, in HH:mm format,eg.09:15我也有时间有人工作过,以 HH:mm 格式,例如 09:15

What is the easiest-fastest way to convert them in dates and find the difference in HH:mm format?将它们转换为日期并找到 HH:mm 格式的差异的最简单、最快的方法是什么? Should i use Moment.js?我应该使用 Moment.js 吗? Also,i need the difference in minutes or seconds so that i can do comparisons with that.另外,我需要以分钟或秒为单位的差异,以便我可以与之进行比较。

I would be grateful if you could give me some examples.如果你能给我一些例子,我将不胜感激。

For finding the difference between the working hours, you could make use of MomentJS.为了找出工作时间之间的差异,您可以使用 MomentJS。

Example:例子:

var working_hours = '08:30-16:30';
var hours_arr = working_hours.split('-'); // Gives an array with ['08:30', '16:30']

var start = moment(hours_arr[0], "HH:mm");
var end = moment(hours_arr[1], "HH:mm");

var duration = moment.duration(end.diff(start));

var minutes = parseInt(duration.asMinutes());

The minutes variable would contain the difference in minutes. minutes变量将包含以分钟为单位的差异。

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

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