美文网首页
Go计算文章发布时间

Go计算文章发布时间

作者: 秀秀小虞儿 | 来源:发表于2022-04-27 16:33 被阅读0次
    func GetProfitTime(creTime int64) string {
        if creTime == 0 {
            return ""
        }
    
        var reTime string
        today, _ := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Local)                       // 今日零点时间戳
        yesterday, _ := time.ParseInLocation("2006-01-02", time.Now().AddDate(0, 0, -1).Format("2006-01-02"), time.Local) // 昨天零点时间戳
        yearTime, _ := time.ParseInLocation("2006", time.Now().Format("2006"), time.Local)
    
        if creTime >= today.Unix() { // 当天
            reTime = time.Unix(creTime, 0).Format("15:04")
        } else if creTime >= yesterday.Unix() && creTime < today.Unix() { // 昨天
            reTime = "昨天 " + time.Unix(creTime, 0).Format("15:04")
        } else if creTime >= yearTime.Unix() && creTime < yesterday.Unix() { // 今年
            reTime = time.Unix(creTime, 0).Format("01-02 15:04:05")
        } else { // 大于一年
            reTime = time.Unix(creTime, 0).Format("2006-01-02 15:04:05")
        }
    
        return reTime
    }
    

    相关文章

      网友评论

          本文标题:Go计算文章发布时间

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