timeutils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package timeutils
  2. import (
  3. "time"
  4. )
  5. var (
  6. monthMap = map[string]string{
  7. "01": "一月",
  8. "02": "二月",
  9. "03": "三月",
  10. "04": "四月",
  11. "05": "五月",
  12. "06": "六月",
  13. "07": "七月",
  14. "08": "八月",
  15. "09": "九月",
  16. "10": "十月",
  17. "11": "十一月",
  18. "12": "十二月",
  19. }
  20. weekMap = map[string]string{
  21. "Monday": "星期一",
  22. "Tuesday": "星期二",
  23. "Wednesday": "星期三",
  24. "Thursday": "星期四",
  25. "Friday": "星期五",
  26. "Saturday": "星期六",
  27. "Sunday": "星期日",
  28. }
  29. )
  30. // ParseLocalDateTime 将日期时间字符串转换为time.Time,dateTime参数如果不是日期时间字符串会panic
  31. func ParseLocalDateTime(dateTime string) *time.Time {
  32. t, err := time.ParseInLocation(time.DateTime, dateTime, time.Local)
  33. if err != nil {
  34. panic(err)
  35. }
  36. return &t
  37. }
  38. // ParseLocalDateOnly 将日期字符串转换为time.Time,dateTime参数如果不是日期字符串会panic
  39. func ParseLocalDateOnly(date string) *time.Time {
  40. t, err := time.ParseInLocation(time.DateOnly, date, time.Local)
  41. if err != nil {
  42. panic(err)
  43. }
  44. return &t
  45. }
  46. // ParseNowDateOnly 将当前时间转换为time.Time,仅有日期信息
  47. func ParseNowDateOnly() *time.Time {
  48. return ParseLocalDateOnly(FormatNowDateOnly())
  49. }
  50. // FormatNowDateTime 将当前时间转化为日期时间格式
  51. func FormatNowDateTime() string {
  52. return time.Now().Format(time.DateTime)
  53. }
  54. // FormatLocalDateTime 将time.Time转化为日期时间格式
  55. func FormatLocalDateTime(t *time.Time) string {
  56. if t == nil {
  57. return ""
  58. }
  59. return t.Format(time.DateTime)
  60. }
  61. // FormatNowDateOnly 将当前时间转化为日期格式
  62. func FormatNowDateOnly() string {
  63. return time.Now().Format(time.DateOnly)
  64. }
  65. // FormatLocalDateOnly 将time.Time转化为日期格式
  66. func FormatLocalDateOnly(t *time.Time) string {
  67. if t == nil {
  68. return ""
  69. }
  70. return t.Format(time.DateOnly)
  71. }
  72. // GetYear 获取time.Time的年
  73. func GetYear(t *time.Time) int {
  74. if t == nil {
  75. return 0
  76. }
  77. year, _ := t.ISOWeek()
  78. return year
  79. }
  80. // GetWeek 获取time.Time的星期
  81. func GetWeek(t *time.Time) int {
  82. if t == nil {
  83. return 0
  84. }
  85. _, week := t.ISOWeek()
  86. return week
  87. }
  88. // DiffNatureDays 计算两个时间相差的天数
  89. func DiffNatureDays(t1str string, t2str string) int {
  90. t1 := ParseLocalDateTime(t1str).Unix()
  91. t2 := ParseLocalDateTime(t2str).Unix()
  92. var secondsOfDay int64 = 86400
  93. if t1 == t2 {
  94. return -1
  95. }
  96. if t1 > t2 {
  97. t1, t2 = t2, t1
  98. }
  99. diffDays := 0
  100. secDiff := t2 - t1
  101. if secDiff > secondsOfDay {
  102. tmpDays := int(secDiff / secondsOfDay)
  103. t1 += int64(tmpDays) * secondsOfDay
  104. diffDays += tmpDays
  105. }
  106. st := time.Unix(t1, 0)
  107. et := time.Unix(t2, 0)
  108. dateFormatTpl := "20060102"
  109. if st.Format(dateFormatTpl) != et.Format(dateFormatTpl) {
  110. diffDays += 1
  111. }
  112. return diffDays
  113. }
  114. // GetChinaMonth 获取中文表示的年
  115. func GetChinaMonth(t *time.Time) string {
  116. if t == nil {
  117. return ""
  118. }
  119. month := t.Format("01")
  120. return monthMap[month]
  121. }
  122. // GetChinaWeek 获取中文表示的星期
  123. func GetChinaWeek(t *time.Time) string {
  124. if t == nil {
  125. return ""
  126. }
  127. week := t.Weekday()
  128. return weekMap[week.String()]
  129. }