time.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package utility
  2. import (
  3. "gadmin/config"
  4. "strings"
  5. "time"
  6. "github.com/jinzhu/now"
  7. )
  8. func GetBeginAndEndOfDay(date string) (begin, end int64, err error) {
  9. t, err := now.Parse(date) // 2017-10-13 00:00:00, nil
  10. if err != nil {
  11. return
  12. }
  13. begin = now.With(t).BeginningOfDay().Unix()
  14. end = now.With(t).EndOfDay().Unix()
  15. return
  16. }
  17. func GetBeginAndEndOfDay2(day, endDay string) (begin, end int64, err error) {
  18. t, err := now.Parse(day) // 2017-10-13 00:00:00, nil
  19. if err != nil {
  20. return
  21. }
  22. t2, err := now.Parse(endDay) // 2017-10-13 00:00:00, nil
  23. if err != nil {
  24. return
  25. }
  26. begin = now.With(t).BeginningOfDay().Unix()
  27. end = now.With(t2).EndOfDay().Unix()
  28. return
  29. }
  30. func Format(date time.Time) string {
  31. return date.In(config.DefaultZone).Format("2006-01-02")
  32. }
  33. func FormatSecond(date time.Time) string {
  34. return date.In(config.DefaultZone).Format("2006-01-02 15:04:05")
  35. }
  36. func ParseDate(date string) string {
  37. if i := strings.Index(date, "T"); i > 0 {
  38. return date[:i]
  39. }
  40. return date
  41. }
  42. func ParseDateToTime(date string) time.Time {
  43. return now.MustParse(ParseDate(date))
  44. }
  45. func ParseUnixStamp(date string) int64 {
  46. if i := strings.Index(date, "T"); i > 0 {
  47. return now.MustParse(date[:i]).Unix()
  48. }
  49. return 0
  50. }
  51. func WeekByDate(t time.Time) (week int) {
  52. yearDay := t.YearDay()
  53. yearFirstDay := t.AddDate(0, 0, -yearDay+1)
  54. firstDayInWeek := int(yearFirstDay.Weekday())
  55. //今年第一周有几天
  56. firstWeekDays := 1
  57. if firstDayInWeek != 0 {
  58. firstWeekDays = 7 - firstDayInWeek + 1
  59. }
  60. if yearDay <= firstWeekDays {
  61. week = 1
  62. } else {
  63. week = (yearDay-firstWeekDays)/7 + 2
  64. }
  65. return week
  66. }
  67. func CurrentWeek() (week int) {
  68. _, week = time.Now().ISOWeek()
  69. return week
  70. }
  71. func WeekIndexDate() string {
  72. n := time.Now()
  73. offset := int(time.Monday - n.Weekday())
  74. if offset > 0 {
  75. offset = -6
  76. }
  77. weekStart := time.Date(n.Year(), n.Month(), n.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  78. return weekStart.Format("20060102")
  79. }
  80. func GetYearDay2(tm *time.Time) int {
  81. return tm.Year()*1000 + tm.YearDay()
  82. }
  83. func GetDaysBetween2Date(format, date1Str, date2Str string) (int, error) {
  84. // 将字符串转化为Time格式
  85. date1, err := time.ParseInLocation(format, date1Str, time.Local)
  86. if err != nil {
  87. return 0, err
  88. }
  89. // 将字符串转化为Time格式
  90. date2, err := time.ParseInLocation(format, date2Str, time.Local)
  91. if err != nil {
  92. return 0, err
  93. }
  94. //计算相差天数
  95. if date1.Unix() > date2.Unix() {
  96. return int(date1.Sub(date2).Hours() / 24), nil
  97. } else {
  98. return int(date2.Sub(date1).Hours() / 24), nil
  99. }
  100. }
  101. func MaxTime(time1, time2 time.Time) time.Time {
  102. if time1.After(time2) {
  103. return time1
  104. }
  105. return time2
  106. }