該文能夠快速在Go語言中得到時間的計算。app
now := time.Now() fmt.Printf("current time is :%s", now) current time is :2009-11-10 23:00:00 +0000 UTC m=+0.000000001
cur_time := time.Now().Unix() fmt.Printf("current unix timestamp is :%v\n", cur_time )
now := time.Now() date := time.Date(now.Year(), now.Month(), now.Day(),0, 0, 0, 0, time.Local) fmt.Printf("date is :%s", date) date is :2021-04-13 00:00:00 +0800
標準時間 time.Now().UTC()
本地時區 time.Now().Local()
this
// 獲取0時區時間 fmt.Printf("date is :%s\n", time.Now().UTC()) date is :2021-04-13 16:02:33.853254 +0000 UTC // 快速設置時區 timeLocation, _ := time.LoadLocation("Asia/Tokyo") //使用時區碼 fmt.Println(time.Now().In(timeLocation).String()) // 快速設置時區 2021-04-14 01:09:18.140997 +0900 JST
time.April type Month int const ( January Month = 1 + iota February March April May June July August September October November December )
time.Sunday type Weekday int const ( Sunday Weekday = iota Monday Tuesday Wednesday Thursday Friday Saturday )
Go中時間格式化的格式爲 2006-01-02 15:04:05
612345爲格式,而不是具體時間unix
dt := time.Now() // YYYY-MM-DD fmt.Println(dt.Format("2006-01-02")) // YYYY-MM-DD hh:mm:ss fmt.Println(dt.Format("2006-01-02 15:04:05")) // 顯示星期英文簡寫 fmt.Println(dt.Format("2006-01-02 15:04:05 Mon")) // 星期的大寫 fmt.Println(dt.Format("2006-01-02 15:04:05 Monday")) // 增長微秒 fmt.Println(dt.Format("2006-01-02 15:04:05.000000")) // 納秒 fmt.Println(dt.Format("2006-01-02 15:04:05.000000000")) } // print result 08-10-2018 08-10-2018 21:11:58 08-10-2018 21:11:58 Fri 08-10-2018 21:11:58 Friday 08-10-2018 21:11:58.880934 08-10-2018 21:11:58.880934320
獲取一個星期的第一天是幾號code
t:=time.Now() fmt.Println(t.Weekday()) // 獲取如今時間爲本週的星期幾
獲得本日爲星期幾後,能夠對時間進行計算,由於time包內星期的常量都爲int,能夠直接進行算數運算.
用一週的第一天減去當日爲星期幾,若是爲0既『本日爲本週的第一天』orm
time.AddDate(year, month, date)
,僅能夠添加年月日
time.Add(Hours, Minutes, Seconds)
,僅能夠添加時分秒string
offset := int(time.Monday - t.Weekday()) //=-1
如不爲0,time包提供了,「以當前時間爲基點,進行加減運算」io
// t.AddDate(year, month, date) t.AddDate(0,0,offset) // 能夠獲取到,週一爲幾月幾日
綜上所屬,能夠得到每週第一天爲幾月幾日,每週隨後一天爲幾月幾日ast
/** * 獲取上週周第一天具體年月日 **/ func GetLastWeekFirstDate() (weekMonday string) { thisWeekMonday := GetFirstDateOfWeek() TimeMonday, _ := time.Parse("2006-01-02", thisWeekMonday) lastWeekMonday := TimeMonday.AddDate(0, 0, -7) weekMonday = lastWeekMonday.Format("2006-01-02") return } /** * 獲取本週的週一具體年月日 **/ func GetFirstDateOfWeek() (weekMonday string) { now := time.Now() offset := int(time.Monday - now.Weekday()) if offset > 0 { offset = -6 } weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset) weekMonday = weekStartDate.Format("2006-01-02") return } /** * 獲取上週最後一天具體年月日 **/ func GetLastWeekLastDate() (weekMonday string) { now := time.Now() offset := int(time.Monday - now.Weekday()) if offset > 0 { offset = -6 } weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset) weekMonday = weekStartDate.AddDate(0, 0, -1).Format("2006-01-02") return } /** * 獲取上週一星期全部天數的具體年月日 **/ func GetBetweenDates(sdate, edate string) []string { d := []string{} timeFormatTpl := "2006-01-02 15:04:05" if len(timeFormatTpl) != len(sdate) { timeFormatTpl = timeFormatTpl[0:len(sdate)] } date, err := time.Parse(timeFormatTpl, sdate) if err != nil { return d } date2, err := time.Parse(timeFormatTpl, edate) if err != nil { return d } if date2.Before(date) { return d } // 輸出日期格式固定 timeFormatTpl = "2006-01-02" date2Str := date2.Format(timeFormatTpl) d = append(d, date.Format(timeFormatTpl)) for { date = date.AddDate(0, 0, 1) dateStr := date.Format(timeFormatTpl) d = append(d, dateStr) if dateStr == date2Str { break } } return d }