I'm trying to get amount of days between two dates in Go but as I'm getting the difference in time (hours) and then divided by the amount of time per day, I have an issue.
Problem: if dates are different (7 of May and 8 of May) but the time between then is lower than 24h, my code counts as there are no days in between.
What I want: count real days in between.
// Days
firstDay := firstDate.Unix()
lastDay := lastDate.Unix()
fmt.Println("firstDay: ", firstDay)
fmt.Println("lastDay: ", lastDay)
if firstDay > lastDay {
fmt.Println("IS TO SMALL")
return
}
// businessDays =
businessDays := (lastDay - firstDay) / 86400
fmt.Println("businessDays: ", businessDays)
Thank you very much.
Get days between dates. T calculate the number of days between two dates you can simply subtract the older date from the newer date. The result will be an integer that represent the days between dates. In the example shown, the formula in D6 is: The result is 365, since there are 365 days between 1/1/1999 and 1/1/2000.
The calculation is performed by counting how many multiples of 86,400,000 milliseconds are between the calendars after both have been set to midnight of their respective days. For example, my function will compute 1 day's difference between a Calendar on January 1, 11:59PM and January 2, 12:01AM.
Counting days requires a time zone. That other Answer uses the Instant which is a moment in UTC, always in UTC. So you are not getting the correct number of calendar days elapsed. To count days by the calendar, convert your legacy Calendar to a ZonedDateTime, then feed to ChronoUnit.DAYS.between.
The DAYS function takes the end date and start date to return the number of days between the two dates. That suits us! Let’s apply the following DAYS function formula to our example:
There are many things to worry about. For example, due to daylight saving time (DST), not all days are 24 hours. What if the times are in different time zones? And so on.
Here's a solution that addresses these issues.
package main
import (
"fmt"
"time"
)
// CalendarDays returns the calendar difference between times (t2 - t1) as days.
func CalendarDays(t2, t1 time.Time) int {
y, m, d := t2.Date()
u2 := time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
y, m, d = t1.In(t2.Location()).Date()
u1 := time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
days := u2.Sub(u1) / (24 * time.Hour)
return int(days)
}
func main() {
first := time.Now().Round(0)
end := first.Add(48 * time.Hour)
for last := first; last.Before(end); last = last.Add(6 * time.Hour) {
fmt.Println("Days:", CalendarDays(last, first), "Last:", last, "First:", first)
}
}
Playground: https://play.golang.org/p/wKwQzgfKa8f
Output:
Days: 0 Last: 2018-02-05 17:38:15.623292254 -0500 EST First: 2018-02-05 17:38:15.623292254 -0500 EST
Days: 0 Last: 2018-02-05 23:38:15.623292254 -0500 EST First: 2018-02-05 17:38:15.623292254 -0500 EST
Days: 1 Last: 2018-02-06 05:38:15.623292254 -0500 EST First: 2018-02-05 17:38:15.623292254 -0500 EST
Days: 1 Last: 2018-02-06 11:38:15.623292254 -0500 EST First: 2018-02-05 17:38:15.623292254 -0500 EST
Days: 1 Last: 2018-02-06 17:38:15.623292254 -0500 EST First: 2018-02-05 17:38:15.623292254 -0500 EST
Days: 1 Last: 2018-02-06 23:38:15.623292254 -0500 EST First: 2018-02-05 17:38:15.623292254 -0500 EST
Days: 2 Last: 2018-02-07 05:38:15.623292254 -0500 EST First: 2018-02-05 17:38:15.623292254 -0500 EST
Days: 2 Last: 2018-02-07 11:38:15.623292254 -0500 EST First: 2018-02-05 17:38:15.623292254 -0500 EST
Duration.Hours()
to get hoursmath.Ceil()
to find calendar daysTime.Truncate()
to truncate hour and minute, only save dayThe source code:
package main
import (
"fmt"
"math"
"time"
)
func main() {
d1, err := time.Parse("200601021504", "201801020001")
if err != nil {
panic(err)
}
d2, err := time.Parse("200601021504", "201801020002")
if err != nil {
panic(err)
}
newD1 := d1.Truncate(time.Hour * 24)
newD2 := d2.Truncate(time.Hour * 24)
fmt.Printf("days: %v\n", math.Ceil(newD2.Sub(newD1).Hours()/24))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With