go中time比较时需要注意写法
package main import ( "fmt" "time" ) func main() { format := "2006-01-02 15:04:05" now := time.Now() //now, _ := time.Parse(format, time.Now().Format(format)) a, _ := time.Parse(format, "2015-03-10 11:00:00") b, _ := time.Parse(format, "2015-03-10 16:00:00") fmt.Println(now.Format(format), a.Format(format), b.Format(format)) fmt.Println(now.After(a)) fmt.Println(now.Before(a)) fmt.Println(now.After(b)) fmt.Println(now.Before(b)) fmt.Println(a.After(b)) fmt.Println(a.Before(b)) fmt.Println(now.Format(format), a.Format(format), b.Format(format)) fmt.Println(now.Unix(), a.Unix(), b.Unix()) }
以上写法,比较结果不正确,换成注释中的写法,结果正确。
原因不太清楚,有人知道不?