Skip to content

Instantly share code, notes, and snippets.

@dohooo
Last active February 20, 2024 00:29
Show Gist options
  • Save dohooo/27d4e2f1fe726fc156f7cb97074d377f to your computer and use it in GitHub Desktop.
Save dohooo/27d4e2f1fe726fc156f7cb97074d377f to your computer and use it in GitHub Desktop.
function calcHours(ranges) {
const timeRanges = ranges.split(',').map(range => range.trim().split('-'));
let totalHours = 0;
timeRanges.forEach(range => {
const [start, end] = range.map(time => {
const [hour, minute] = time.split(':').map(Number);
return new Date(2000, 0, 1, hour, minute);
});
let diff = (end - start) / (1000 * 60 * 60);
if (diff < 0) {
diff += 24;
}
totalHours += diff;
});
return totalHours;
}
if (process.argv.length > 2) {
const input = process.argv[2];
const hours = calcHours(input);
console.log(hours);
} else {
console.log("Please provide the time ranges as arguments, separated by commas.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment