Golang获取明日时间及距离明日凌晨的时间差

demo 1
    
func getTodaySurplusSecond1() (int64,error) {
    layout := "2006-01-02"
    loc, _ := time.LoadLocation("Asia/Shanghai")
    t, err := time.ParseInLocation(layout, time.Now().Format(layout), loc)

    if err != nil {
        return 0, err
    }
    return int64(86400 - (time.Now().Unix() - t.Unix())), nil
}

Demo2
func getTodaySurplusSecond2() int {
    var cstSh, _ = time.LoadLocation("Asia/Shanghai") //上海
    timeTemplate := "2006-01-02 15:04:05"
    timeStr := time.Now().Format("2006-01-02")
    now_time_str := time.Now().In(cstSh).Format(timeTemplate)
    today_end_time_str := timeStr + " 23:59:59"

    formatTime1,_:=time.Parse(timeTemplate,now_time_str)
    formatTime2,_:=time.Parse(timeTemplate,today_end_time_str)

    t1 := formatTime1.Unix()
    t2 := formatTime2.Unix()
       
        return t2-t1
}
    

 

posted @ 2020-10-09 17:47  温柔的风  阅读(1127)  评论(0编辑  收藏  举报