使用GoLang获取本周上周的时间
package main
import (
"fmt"
"time"
)
func main() {
var err error
now := 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"))
}
这篇博客详细介绍了如何利用GoLang编程语言来获取当前日期对应的本周和上周时间,对于需要处理时间范围的开发者来说非常实用。
286

被折叠的 条评论
为什么被折叠?



