Browse Source

加入本月开始时间戳方法

master v0.1.8
listen 2 years ago
parent
commit
ec4eb3f122
2 changed files with 31 additions and 1 deletions
  1. +30
    -0
      time.go
  2. +1
    -1
      time_test.go

+ 30
- 0
time.go View File

@ -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()
}

+ 1
- 1
time_test.go View File

@ -17,5 +17,5 @@ func Test_FormatDate(t *testing.T) {
}
func Test_GetToadyStartTimeStamp(t *testing.T){
t.Log(GetTodayStartTimeStamp())
t.Log(GetMonthStartTimeStamp())
}

Loading…
Cancel
Save