0

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 } 

1 Comment

@RAFJR look at Verrans answer above. That's the correct way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.