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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容