Go 时间操作

背景

go的一些常用时间获取

代码地址

https://github.com/FakerGit/go-tools/tree/master/times

代码

//当前时间戳
func Now() int64 {
    return time.Now().Unix()
}

//当前时间格式输出
func NowFormat(format string) string {
    return time.Now().Format(format)
}

//今天星期几
func NowWeekday() string {
    return time.Now().Weekday().String()
}

//day

//Get the timestamp of the midnight , pay attention to the time zone
//查询当天零点时间戳,注意时区,减去八个小时
func GetTodayStartTs() (int64, error) {
    t, err := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))
    if err != nil {
        return 0, err
    }
    return t.Unix() - LocationTimes, nil
}

//week
//查询本周周一零点时间
func GetNowMonday() time.Time {
    now := time.Now()
    offset := int(time.Monday - now.Weekday())
    if offset > 0 {
        offset = -6
    }

    monday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
    return monday
}


To be continue

你可能感兴趣的:(Go 时间操作)