date.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package pkg
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. )
  7. func GetTodayZeroTime() time.Time {
  8. nowTime := time.Now()
  9. todayZeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
  10. return todayZeroTime
  11. }
  12. func GetTomorrowZeroTime() time.Time {
  13. nowTime := time.Now()
  14. todayZeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
  15. duration, _ := time.ParseDuration("24h")
  16. tomorrowZeroTime := todayZeroTime.Add(duration)
  17. return tomorrowZeroTime
  18. }
  19. func GetFormatTime(origin time.Time) string {
  20. if origin.Year() == 0001 { // 空值处理
  21. return ""
  22. }
  23. return origin.Format("2006-01-02 15:04:05")
  24. }
  25. func GetMySQLFormatTime(origin time.Time) string {
  26. if origin.Year() == 0001 { // 空值处理
  27. return ""
  28. }
  29. return origin.Format("2006_01_02")
  30. }
  31. func IsEmptyTime(origin time.Time) bool {
  32. if origin.Year() == 0001 { // 空值处理
  33. return true
  34. } else {
  35. return false
  36. }
  37. }
  38. func GetLastMonthFirstDay() time.Time {
  39. timeNow := time.Now()
  40. curMonthFirstDay := TimeParse(timeNow.Year(), timeNow.Month(), 1) // 获取本月第一天0点
  41. duration, _ := time.ParseDuration("-1h")
  42. lastMonthDay := curMonthFirstDay.Add(duration) // 获取到上个月的时间
  43. return TimeParse(lastMonthDay.Year(), lastMonthDay.Month(), 1)
  44. }
  45. func TimeParse(year int, month time.Month, day int) time.Time {
  46. location, err := time.LoadLocation("Asia/Shanghai")
  47. if err != nil {
  48. location = time.FixedZone("CST-8", 8*3600)
  49. }
  50. time, _ := time.ParseInLocation("2006-01-02 15:04:05", FormatDate(year, month, day), location)
  51. return time
  52. }
  53. func TimeParseFmt(timeStr string) time.Time {
  54. location, err := time.LoadLocation("Asia/Shanghai")
  55. if err != nil {
  56. location = time.FixedZone("CST-8", 8*3600)
  57. }
  58. time, _ := time.ParseInLocation("2006-01-02 15:04:05", timeStr, location)
  59. return time
  60. }
  61. func FormatDate(year int, month time.Month, day int) string {
  62. return fmt.Sprintf("%d-%s-%s 00:00:00", year, monthToDate(month), intToDate(day))
  63. }
  64. func intToDate(num int) string {
  65. if num < 10 {
  66. return fmt.Sprintf("0%d", num)
  67. } else {
  68. return strconv.Itoa(num)
  69. }
  70. }
  71. func monthToDate(month time.Month) string {
  72. switch month {
  73. case time.January:
  74. return "01"
  75. case time.February:
  76. return "02"
  77. case time.March:
  78. return "03"
  79. case time.April:
  80. return "04"
  81. case time.May:
  82. return "05"
  83. case time.June:
  84. return "06"
  85. case time.July:
  86. return "07"
  87. case time.August:
  88. return "08"
  89. case time.September:
  90. return "09"
  91. case time.October:
  92. return "10"
  93. case time.November:
  94. return "11"
  95. case time.December:
  96. return "12"
  97. }
  98. return ""
  99. }