golang 自定时间区间加上时区

我们经常需要获取前一一天 一个月等需求

自定义时间

currentYear, currentMonth, currentDay := now.Date()

自定义时区

loc, _ := time.LoadLocation("Asia/Chongqing")

格式化时间

layout := "2006-01-02 15:04:05"

自定义时间

firstOfDay := time.Date(currentYear, currentMonth, currentDay, 0, 0, 0, 0, loc)
lastOfDay := time.Date(currentYear, currentMonth, currentDay, 23, 59, 59, 0, loc)

自定义昨天

        now := time.Now().AddDate(-1, 0, 0)
	currentYear, currentMonth, currentDay := now.Date()
	loc, _ := time.LoadLocation("Asia/Chongqing")
	layout := "2006-01-02 15:04:05"

	firstOfDay := time.Date(currentYear, currentMonth, currentDay, 0, 0, 0, 0, loc)
	lastOfDay := time.Date(currentYear, currentMonth, currentDay, 23, 59, 59, 0, loc)

	fmt.Printf("firstOfMonth:%s\n", firstOfDay.In(loc).Format(layout))
	fmt.Printf("lastOfMonth:%s\n", lastOfDay.In(loc).Format(layout))
posted @ 2022-07-14 11:13  vx_guanchaoguo0  阅读(142)  评论(0编辑  收藏  举报