Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sambello247/0fc92468737c50352eeb9eda101cdc72 to your computer and use it in GitHub Desktop.

Select an option

Save sambello247/0fc92468737c50352eeb9eda101cdc72 to your computer and use it in GitHub Desktop.
how to subtract two time string (HH:MM) in javascript
var time1= "03:01";
var time2= "01:19";
function strToMins(t) {
var s = t.split(":");
return Number(s[0]) * 60 + Number(s[1]);
}
function minsToStr(t) {
return Math.trunc(t / 60)+':'+('00' + t % 60).slice(-2);
}
var result = minsToStr( strToMins(time1) - strToMins(time2) );
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment