date.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package myth
  2. import (
  3. "time"
  4. )
  5. const (
  6. // TimeFmtLong yyyy-MM-dd hh:mm:ss
  7. TimeFmtLong = `2006-01-02 15:04:05`
  8. // TimeFmtNumeric yyyyMMddhhmmss
  9. TimeFmtNumeric = `20060102150405`
  10. // DateFmtLong yyyy-MM-dd
  11. DateFmtLong = `2006-01-02`
  12. // DateFmtNumeric yyyyMMdd
  13. DateFmtNumeric = `20060102`
  14. )
  15. // IsTime 是否时间格式字符串
  16. func IsTime(s string) bool {
  17. if _, err := time.Parse(TimeFmtLong, s); err != nil {
  18. return false
  19. }
  20. return true
  21. }
  22. // IsDate 是否为有效日期
  23. func IsDate(s string) bool {
  24. if _, e := time.Parse(`2006-01-02`, s); e != nil {
  25. return false
  26. }
  27. return true
  28. }
  29. // StrToTime 字符串转时间
  30. func StrToTime(s string) (t time.Time, err error) {
  31. t, err = time.Parse(TimeFmtLong, s)
  32. return
  33. }
  34. // StrFmtTime 时间转字符串
  35. func StrFmtTime(s, fmt string) (t time.Time, err error) {
  36. t, err = time.Parse(fmt, s)
  37. return
  38. }
  39. // TimeToStr 时间转字符串
  40. func TimeToStr(t time.Time) string {
  41. return t.Format(TimeFmtLong)
  42. }
  43. // TimeFmtStr 时间转字符串
  44. func TimeFmtStr(t time.Time, fmt string) string {
  45. return t.Format(fmt)
  46. }
  47. // StrToDate 字符串转日期
  48. func StrToDate(s string) (t time.Time, err error) {
  49. t, err = time.Parse(DateFmtLong, s)
  50. return
  51. }
  52. // IsWeekEnd 日期是否周末
  53. func IsWeekEnd(d time.Weekday) bool {
  54. day := int(d)
  55. if day == 6 || day == 0 {
  56. return true
  57. }
  58. return false
  59. }