美文网首页Go
Go 时间操作

Go 时间操作

作者: HollowKnight | 来源:发表于2019-05-11 17:33 被阅读0次

    背景

    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 时间操作

        本文链接:https://www.haomeiwen.com/subject/jtffaqtx.html