package main
import (
"fmt"
"time"
)
func main(){
timeFormatDay := "2006-01-02"
nowTime := time.Now()
endTime := nowTime
yesterdayTime := nowTime.AddDate(0, 0, -1)
beginTime, _ := time.ParseInLocation(timeFormatDay, yesterdayTime.Format(timeFormatDay), time.Local)
fmt.Printf("%v\n%v\n", beginTime, endTime)
fmt.Println(time.Since(yesterdayTime))
timeFormat := "2006-01-02 15:04:05"
begin, _ := time.ParseInLocation(timeFormat, "2020-06-09 01:00:00", time.Local)
end, _ := time.ParseInLocation(timeFormat, "2020-06-10 01:00:00", time.Local)
fmt.Printf("%v\n%v\n", begin, end)
}
注意:
1.上述time format中的值是固定的,只能那样,调成2007或者其它的,就会出错。
2. 要用ParseInLocation, 而不要直接用Parse, 因为后者会丢失当地时区的概念。