time.Weekday類型能夠作運算,強制轉int,會獲得誤差數。
默認是 Sunday 開始到 Saturday 算 0,1,2,3,4,5,6code
因此只有Monday減去Sunday的時候是正數,特殊處理下就能夠了。import
package date import ( "fmt" "time" ) func WeekDayTest() { now := time.Now() offset := int(time.Monday - now.Weekday()) if offset > 0 { offset = -6 } weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset) fmt.Println(weekStart) }