golang 获取本周 上周的时间
生活随笔
收集整理的這篇文章主要介紹了
golang 获取本周 上周的时间
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用GoLang獲取本周上周的時間
package mainimport ("fmt""time" )func main() {var err errornow := time.Now()now, err = time.Parse("2006-01-02", "2021-05-01")if err != nil {fmt.Println("err = ", err)}fmt.Println("now = ", now.Format("2006/01/02 15:04:05"))fmt.Println("today = ", now.Format("2006/01/02"))offset := int(time.Monday - now.Weekday())if offset > 0 {offset = -6}fmt.Println("offset = ", offset)fmt.Println("this week start = ", now.AddDate(0, 0, offset).Format("2006-01-02"))fmt.Println("this week end = ", now.AddDate(0, 0, offset+6).Format("2006-01-02"))fmt.Println("last week start = ", now.AddDate(0, 0, offset-7).Format("2006-01-02"))fmt.Println("last week end = ", now.AddDate(0, 0, offset-1).Format("2006-01-02")) }總結(jié)
以上是生活随笔為你收集整理的golang 获取本周 上周的时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开启七牛云CDN免费HTTPS支持
- 下一篇: IT与DT技术几点解释