Go计算文章发布时间

2022-04-27  本文已影响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
}
上一篇下一篇

猜你喜欢

热点阅读