time.go 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package utils
  2. import (
  3. "time"
  4. )
  5. // ToDate 转化为日期
  6. func ToDate(second int64) string {
  7. return time.Unix(second, 0).Format(time.DateOnly)
  8. }
  9. // ToDateTime 转化为日期时间
  10. func ToDateTime(second int64) string {
  11. return time.Unix(second, 0).Format(time.DateTime)
  12. }
  13. // CurDate 获取当前日期
  14. func CurDate() string {
  15. return time.Now().Format(time.DateOnly)
  16. }
  17. // CurDateTime 获取当前日期时间
  18. func CurDateTime() string {
  19. return time.Now().Format(time.DateTime)
  20. }
  21. func ToSecond(date, layout string) int64 {
  22. dateTime, err := time.Parse(layout, date)
  23. if err != nil {
  24. return 0
  25. }
  26. return dateTime.Unix()
  27. }
  28. func DateAdd(date string, day int64) string {
  29. dateTime, err := time.Parse(time.DateOnly, date)
  30. if err != nil {
  31. return ""
  32. }
  33. return dateTime.Add(time.Duration(day) * 24 * time.Hour).Format(time.DateOnly)
  34. }
  35. func CurTimestamp() int64 {
  36. return time.Now().Unix()
  37. }
  38. // Second int64转换成秒类型
  39. func Second(s int64) time.Duration {
  40. return time.Duration(s) * time.Second
  41. }