time.go 663 B

123456789101112131415161718192021222324252627282930313233
  1. package Utils
  2. import (
  3. "strconv"
  4. "time"
  5. )
  6. func TimeFormat(unix int) (string, string) {
  7. timeInt := time.Unix(int64(unix), 0)
  8. return timeInt.Format("2006年01月02日"), timeInt.Format("2006-01-02 15:04:05")
  9. }
  10. func DateFormat(times int) string {
  11. createTime := time.Unix(int64(times), 0)
  12. now := time.Now().Unix()
  13. difTime := now - int64(times)
  14. str := ""
  15. if difTime < 60 {
  16. str = "刚刚"
  17. } else if difTime < 3600 {
  18. M := difTime / 60
  19. str = strconv.Itoa(int(M)) + "分钟前"
  20. } else if difTime < 3600*24 {
  21. H := difTime / 3600
  22. str = strconv.Itoa(int(H)) + "小时前"
  23. } else {
  24. str = createTime.Format("2006-01-02 15:04:05")
  25. }
  26. return str
  27. }