Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get real calendar days between two dates

Tags:

go

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.

like image 966
Trody Avatar asked Feb 05 '18 14:02

Trody


People also ask

How do you get the number of days between two dates?

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.

How do I calculate the difference between two calendars?

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.

How to count the number of days in a calendar?

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.

What is the days function in Excel?

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:


2 Answers

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
like image 41
peterSO Avatar answered Oct 04 '22 13:10

peterSO


  1. Use Duration.Hours() to get hours
  2. Use math.Ceil() to find calendar days
  3. Use Time.Truncate() to truncate hour and minute, only save day

The 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))

}
like image 108
zouying Avatar answered Oct 04 '22 14:10

zouying