date.go 1.5 KB

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