简体   繁体   中英

Timesheet hours difference in moment.js

I have a string '08:30-16:30' describing working hours. I also have the time someone has worked, in HH:mm format,eg.09:15

What is the easiest-fastest way to convert them in dates and find the difference in HH:mm format? Should i use 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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