fmt.go 891 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package tyr
  2. import (
  3. "time"
  4. )
  5. // Time fmt
  6. const (
  7. TimeFmtLong = `2006-01-02 15:04:05` // yyyy-MM-dd hh:mm:ss
  8. TimeFmtNumeric = `20060102150405` // yyyyMMddhhmmss
  9. DateFmtLong = `2006-01-02` // yyyy-MM-dd
  10. DateFmtNumeric = `20060102` // yyyyMMdd
  11. )
  12. // IsTime 是否时间格式字符串
  13. func IsTime(s string) bool {
  14. if _, err := time.Parse(TimeFmtLong, s); err != nil {
  15. return false
  16. }
  17. return true
  18. }
  19. // Str2Time 字符串转时间
  20. func Str2Time(s string) (t time.Time, err error) {
  21. t, err = time.Parse(TimeFmtLong, s)
  22. return
  23. }
  24. // StrFmtTime 时间转字符串
  25. func StrFmtTime(s, fmt string) (t time.Time, err error) {
  26. t, err = time.Parse(fmt, s)
  27. return
  28. }
  29. // Time2Str 时间转字符串
  30. func Time2Str(t time.Time) string {
  31. return t.Format(TimeFmtLong)
  32. }
  33. // TimeFmtStr 时间转字符串
  34. func TimeFmtStr(t time.Time, fmt string) string {
  35. return t.Format(fmt)
  36. }