Calculating total number of Hours, Days, Minutes and Seconds between two dates in Golang
To get the number of days between two dates, we can calculate the number of days from the beginning 0/0/0000 to the first date and the second date and compute their difference.
Input: 2020-04-27 23:35:00, 2018-5-12 12:43:23 Output: 716 days, 10 hours, 51 minutes, 37 seconds Input: 2014-02-25 05:50:20, 2003-12-13 21:42:12 Output: 3726 days, 8 hours, 8 minutes, 8 seconds
To calculate the time difference, we can calculate hours2-hours1, minutes2-minutes1, seconds2-seconds1, and perform some operations (shown in the code from line 65 to line 80) to avoid negative values.
716 days, 10 hours, 51 minutes, 37 seconds
My Personal Notes arrow_drop_up
Please Login to comment...