From 0d726b8da2914ed41bd4e5cf8c88d17cbbfe67dd Mon Sep 17 00:00:00 2001 From: listen <494177603@qq.com> Date: Mon, 9 Aug 2021 14:24:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=BD=93=E5=A4=A9=E5=BC=80=E5=A7=8B=E6=97=B6=E9=97=B4=E6=88=B3?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- string_test.go | 1 + time.go | 11 +++++++++++ time_test.go | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/string_test.go b/string_test.go index 1d33ced..0395baf 100755 --- a/string_test.go +++ b/string_test.go @@ -1,6 +1,7 @@ package helper import ( + "fmt" "testing" ) diff --git a/time.go b/time.go index d14b246..ae84828 100755 --- a/time.go +++ b/time.go @@ -19,3 +19,14 @@ func FormatDate(str string) string { } return time.Unix(date, 0).Format("2006-01-02") } + +//获取当天开始时间戳 +func GetTodayStartTimeStamp() int64{ + + var reserveTime time.Time + loc, _ := time.LoadLocation("Asia/Shanghai") + + date := time.Now().Format("2006-01-02") + reserveTime,_ = time.ParseInLocation("2006-01-02",date,loc) + return reserveTime.Unix() +} diff --git a/time_test.go b/time_test.go index 96a4b63..e2bab21 100755 --- a/time_test.go +++ b/time_test.go @@ -15,3 +15,7 @@ func Test_FormatDate(t *testing.T) { t.Log(ret.Unix()) t.Log(err) } + +func Test_GetToadyStartTimeStamp(t *testing.T){ + t.Log(GetTodayStartTimeStamp()) +} \ No newline at end of file From ec4eb3f122ff45aae48b8c8e21a4a0d13b405af4 Mon Sep 17 00:00:00 2001 From: listen <494177603@qq.com> Date: Mon, 9 Aug 2021 14:45:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=9C=AC=E6=9C=88?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E6=97=B6=E9=97=B4=E6=88=B3=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- time.go | 30 ++++++++++++++++++++++++++++++ time_test.go | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/time.go b/time.go index ae84828..4ad0d59 100755 --- a/time.go +++ b/time.go @@ -30,3 +30,33 @@ func GetTodayStartTimeStamp() int64{ reserveTime,_ = time.ParseInLocation("2006-01-02",date,loc) return reserveTime.Unix() } + +//获取本周开始时间戳 +func GetWeekStartTimeStamp() int64{ + + var reserveTime time.Time + now := time.Now() + loc, _ := time.LoadLocation("Asia/Shanghai") + + offset := int(time.Monday - now.Weekday()) + if offset > 0 { + offset = -6 + } + + weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset) + weekMonday := weekStartDate.Format("2006-01-02") + + reserveTime,_ = time.ParseInLocation("2006-01-02",weekMonday,loc) + return reserveTime.Unix() +} + +//获取当月开始时间戳 +func GetMonthStartTimeStamp() int64{ + + var reserveTime time.Time + loc, _ := time.LoadLocation("Asia/Shanghai") + + date := time.Now().Format("2006-01") + reserveTime,_ = time.ParseInLocation("2006-01",date,loc) + return reserveTime.Unix() +} diff --git a/time_test.go b/time_test.go index e2bab21..8eda60a 100755 --- a/time_test.go +++ b/time_test.go @@ -17,5 +17,5 @@ func Test_FormatDate(t *testing.T) { } func Test_GetToadyStartTimeStamp(t *testing.T){ - t.Log(GetTodayStartTimeStamp()) + t.Log(GetMonthStartTimeStamp()) } \ No newline at end of file