Created
August 7, 2020 18:20
-
-
Save mreis1/8ae987002f1ea280c9c634bf17fbdae5 to your computer and use it in GitHub Desktop.
Calculate number of days and time between two dates
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
| /** | |
| diffDates returning number of days and time string | |
| */ | |
| function diffDates(a,b) { | |
| var d = moment(a).diff(b); | |
| var m = d / (3600*1000*24); | |
| var m1 = Math.floor(m); | |
| var t = d-(m1*3600*1000*24); | |
| return { | |
| days: m1, | |
| time: t, | |
| timeStr: moment(t).utc().format('HH:mm') | |
| } | |
| } | |
| diffDates( | |
| moment(), moment().add(-1,'day').add(-10,'minutes')) | |
| // Output = 1day e 10minutos | |
| {days: 1, time: 600000, timeStr: "00:10"} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment