I'm looking for a clean way to do this in go:
package main
import(
"fmt"
t "time"
)
func isSameDate() bool {
timeA, err := t.Parse("01022006 15:04", "08152016 09:00")
timeB, err := t.Parse("01022006 15:04", "08152016 07:30")
return timeA.Date == timeB.Date // This is C# code but I'm looking for something similar in GO
}
Should return true
If you use the Truncate
function from the time package you can cut it down to the date.
So truncate both times with the parameter 24 * time.Hour
to get the date of each and compare with the Equal
function.
timeA, _ := t.Parse("01022006 15:04", "08152016 09:00")
timeB, _ := t.Parse("01022006 15:04", "08152016 07:30")
return timeA.Truncate(24 * time.Hour).Equal(timeB.Truncate(24*time.Hour))
Here's the sample: https://play.golang.org/p/39ws4DL2pB
Could call timeA.Date()
format that to a string and compare them like in the following example. Don't know if there's a better way.
func isSameDate() bool {
timeA, _ := t.Parse("01022006 15:04", "08152016 09:00")
timeB, _ := t.Parse("01022006 15:04", "08152016 07:30")
ay, am, ad := timeA.Date()
a := fmt.Sprintf("%d%d%d", ay, am, ad)
by, bm, bd := timeB.Date()
b := fmt.Sprintf("%d%d%d", by, bm, bd)
return a == b
}