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))
本文来自博客园,作者:vx_guanchaoguo0,转载请注明原文链接:https://www.cnblogs.com/guanchaoguo/p/16476978.html