Skip to content

Instantly share code, notes, and snippets.

@Abdul007Malik
Created June 4, 2020 14:54
Show Gist options
  • Save Abdul007Malik/227432c1cd2fc2c1292065f061bf0f5b to your computer and use it in GitHub Desktop.
Save Abdul007Malik/227432c1cd2fc2c1292065f061bf0f5b to your computer and use it in GitHub Desktop.
Timesheet calculation
function calculate(args) {
let time = null, dump, minutes;
dump = args.split(',')
minutes = dump.reduce((total, timeDiff) => {
time = timeDiff.split('-')
total += moment(time[1], "hh:mm").diff(moment(time[0], "hh:mm"), 'minutes')
return total;
}, 0);
return { minutes, hhmm: moment.utc(minutes * 60 * 1000).format('hh:mm') }
}
@Abdul007Malik
Copy link
Author

Timesheet calculation

Dependency: Momentjs

Example: My 28 May work:- 09:00-13:27, 17:50-19:18, 20:30-21:00

calculate('09:00-13:27,17:50-19:18,20:00-20:30');

Output: {minutes: 385, hhmm: "06:25"}


Side Note: If timesheet contains, time between two days say 23:30-02:30, then you will not get the expected output instead you can change this to 23:30-23:59,00:00-02:30, so now calling the calculate function will be as follows:

calculate('09:00-13:27,17:50-19:18,20:00-20:30,23:30-23:59,00:00-02:30');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment