1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| package main
import ( "fmt" "time" )
func main() { now := time.Now()
time1 := time.Now().Format("2006-01-02 15:04:05")
time2 := now.Unix() time3 := time.Unix(time2, 0).Format("2006-01-02 15:04:05")
time4 := now.Add(time.Minute * time.Duration(1)) time5 := now.Add(-(time.Minute * time.Duration(1)))
time6, _ := time.ParseInLocation("2006-01-02 15:04:05", time3, time.Local) if now.Before(time6) { fmt.Println("true") } else if now.After(time6) { fmt.Println("false") }
on_monday, next_monday := getBgnEnd(now) fmt.Println(on_monday, next_monday)
next_month_0 := GetNextMonthStartTs() fmt.Println(next_month_0)
on_month_0 := GetFirstDateOfMonth(now) fmt.Println(on_month_0) }
func getBgnEnd(curT time.Time) (string, string) { curW := int(curT.Weekday()) - 1 if curW == -1 { curW = 6 } curM := curT.AddDate(0, 0, -curW) tmpMondayTime := time.Date(curM.Year(), curM.Month(), curM.Day(), 0, 0, 0, 0, time.Local).Format("2006-01-02 15:04:05") tmpMondayStr := curM.Format("20060102") tmpT, _ := time.ParseInLocation("20060102", tmpMondayStr, time.Local) tmpDeadLineTime := tmpT.AddDate(0, 0, 7).Format("2006-01-02 15:04:05") return tmpMondayTime, tmpDeadLineTime }
func getBgnEnd1(curT time.Time) (string, time.Time) { tmpDayStr := curT.Format("20060102") tmpT, _ := time.ParseInLocation("20060102", tmpDayStr, time.Local) tmpDeadLineTime := tmpT.AddDate(0, 0, 1) return tmpDayStr, tmpDeadLineTime }
func GetNextMonthStartTs() time.Time { now := time.Now() nowYeah, nowMonth, _ := now.Date() loc := now.Location() return time.Date(nowYeah, nowMonth, 1, 0, 0, 0, 0, loc).AddDate(0, 1, 0) }
func GetFirstDateOfMonth(d time.Time) time.Time { d = d.AddDate(0, 0, -d.Day()+1) return GetZeroTime(d) }
func GetZeroTime(d time.Time) time.Time { return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location()) }
|