比较两个日期而不考虑时间

I have two dates like this, I would like to compare only the dates, ignoring the time. Currently I have this:

package main

import (
    "time"
    //"fmt"
)

func main() {
    a, _ := time.Parse(time.RFC3339, "2017-02-01T12:00:00+00:00")
    b, _ := time.Parse(time.RFC3339, "2017-02-11T14:30:00+00:00")

    x := b.Sub(a)

    println(int(x.Hours()))
}

Which prints 242. That is correct, but what I actually want to do is compare the dates like this:

    a, _ := time.Parse(time.RFC3339, "2017-02-01T00:00:00+00:00")
    b, _ := time.Parse(time.RFC3339, "2017-02-11T00:00:00+00:00")

Notice: minutes/hours/seconds have been set to zero - the diff will now be 240 hours.

I couldn't really figure out how to do this, is there a time.SetTime(0, 0, 0) function in Go that I missed or what's the canonical way to reset the time for a date?

You could Truncate the times to make them round to a multiple of a day.

In your example:

oneDay := 24 * time.Hour
a = a.Truncate(oneDay)
b = b.Truncate(oneDay)

Find a playground with the adapted code here: https://play.golang.org/p/yWIYt3UkiT

You have to be rather careful handling dates if using time.Time because there can be edge cases around the beginning and ending of daylight savings.

Shameless plug: I wrote a Date package (actually it was derived from someone else's earlier work) to deal with dates without incurring the DST issues.

https://github.com/rickb777/date

You can do, for example,

a := date.New(2017, 2, 1)
b := date.New(2017, 2, 11)
daysDifference := b.Sub(a)
fmt.Println(daysDifference)

There are other sub-packages to handle date ranges, timespans, ISO8601 periods and clock-face time.