From 0575f2749e312c79e8690f38ee7e615b782da7a5 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Sat, 23 Dec 2023 01:37:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/utils/time.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/utils/time.go b/app/utils/time.go index 17f4690..db38b79 100644 --- a/app/utils/time.go +++ b/app/utils/time.go @@ -168,13 +168,14 @@ func GetTimeRange(s string) map[string]int64 { // 明天 0点 etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) case "current_month": - stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location()) - etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location()) + stime = GetFirstDateOfMonth(t) + etime = time.Now() case "last_month": - stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location()) - etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location()) + etime = GetFirstDateOfMonth(t) + monthTimes := TimeStdParseUnix(etime.Format("2006-01-02 15:04:05")) - 86400 + times, _ := UnixToTime(Int64ToStr(monthTimes)) + stime = GetFirstDateOfMonth(times) } - return map[string]int64{ "start": stime.Unix(), "end": etime.Unix(), @@ -215,6 +216,13 @@ func GetFirstDateOfMonth(d time.Time) time.Time { return GetZeroTime(d) } +//时间戳转时间格式 +func UnixToTime(e string) (datatime time.Time, err error) { + data, err := strconv.ParseInt(e, 10, 64) + datatime = time.Unix(data, 0) + return +} + //获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。 func GetLastDateOfMonth(d time.Time) time.Time { return GetFirstDateOfMonth(d).AddDate(0, 1, -1)