Created
January 28, 2023 12:59
-
-
Save sambello247/0fc92468737c50352eeb9eda101cdc72 to your computer and use it in GitHub Desktop.
how to subtract two time string (HH:MM) in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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