fmt.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // IsDate 是否为有效日期
  20. func IsDate(s string) bool {
  21. if _, e := time.Parse(DateFmtLong, s); e != nil {
  22. return false
  23. }
  24. return true
  25. }
  26. // Str2Date 字符串转日期
  27. func Str2Date(s string) (t time.Time, err error) {
  28. t, err = time.Parse(DateFmtLong, s)
  29. return
  30. }
  31. // IsWeekEnd 日期是否周末
  32. func IsWeekEnd(d time.Weekday) bool {
  33. day := int(d)
  34. if day == 6 || day == 0 {
  35. return true
  36. }
  37. return false
  38. }
  39. // Str2Time 字符串转时间
  40. func Str2Time(s string) (t time.Time, err error) {
  41. t, err = time.Parse(TimeFmtLong, s)
  42. return
  43. }
  44. // StrFmtTime 时间转字符串
  45. func StrFmtTime(s, fmt string) (t time.Time, err error) {
  46. t, err = time.Parse(fmt, s)
  47. return
  48. }
  49. // Time2Str 时间转字符串
  50. func Time2Str(t time.Time) string {
  51. return t.Format(TimeFmtLong)
  52. }
  53. // TimeFmtStr 时间转字符串
  54. func TimeFmtStr(t time.Time, fmt string) string {
  55. return t.Format(fmt)
  56. }