timeTool.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package public
  2. import (
  3. "strings"
  4. "time"
  5. )
  6. func GetMonthFirstLastDay(now time.Time) (firstOfMonth int, lastOfMonth int) {
  7. currentYear, currentMonth, _ := now.Date()
  8. currentLocation := now.Location()
  9. firstDate := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  10. lastDate := firstDate.AddDate(0, 1, -1)
  11. return firstDate.Day(), lastDate.Day()
  12. }
  13. func GetMonthFirstLastTime(now time.Time) (time.Time, time.Time) {
  14. currentYear, currentMonth, _ := now.Date()
  15. currentLocation := now.Location()
  16. firstDate := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  17. lastDate := firstDate.AddDate(0, 1, -1)
  18. return firstDate, lastDate
  19. }
  20. //NotPassToday 是否超过 今天凌晨
  21. func NotPassToday(strTime string) (bool, error) {
  22. //获取转换后的时间
  23. compareTime, err := time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
  24. if err != nil {
  25. return false, err
  26. }
  27. //获取凌晨时间
  28. todayTime, err := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Local)
  29. if err != nil {
  30. return false, err
  31. }
  32. return compareTime.Before(todayTime), nil
  33. }
  34. //日期转字符串
  35. func FormatDate(date time.Time, dateStyle string) string {
  36. dateStyle = strings.Replace(dateStyle, "yyyy", "2006", 1)
  37. dateStyle = strings.Replace(dateStyle, "yy", "06", 1)
  38. dateStyle = strings.Replace(dateStyle, "MM", "01", 1)
  39. dateStyle = strings.Replace(dateStyle, "dd", "02", 1)
  40. dateStyle = strings.Replace(dateStyle, "HH", "15", 1)
  41. dateStyle = strings.Replace(dateStyle, "mm", "04", 1)
  42. dateStyle = strings.Replace(dateStyle, "ss", "05", 1)
  43. dateStyle = strings.Replace(dateStyle, "SSS", "000", -1)
  44. return date.Format(dateStyle)
  45. }