time.go 982 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package utils
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/jinzhu/now"
  6. )
  7. const DateFormat = "2006-01-02"
  8. const DateTimeFormat = "2006-01-02 15:04:05"
  9. func GetBeginAndEndOfDay(date string) (begin, end int64, err error) {
  10. t, err := now.Parse(date) // 2017-10-13 00:00:00, nil
  11. if err != nil {
  12. return
  13. }
  14. begin = now.With(t).BeginningOfDay().Unix()
  15. end = now.With(t).EndOfDay().Unix()
  16. return
  17. }
  18. func Format(date time.Time) string {
  19. return date.Format("2006-01-02")
  20. }
  21. func FormatSecond(date time.Time) string {
  22. return date.Format("2006-01-02 15:04:05")
  23. }
  24. func ParseDate(date string) string {
  25. if i := strings.Index(date, "T"); i > 0 {
  26. return date[:i]
  27. }
  28. return date
  29. }
  30. func ParseDateToTime(date string) time.Time {
  31. return now.MustParse(ParseDate(date))
  32. }
  33. func ParseUnixStamp(date string) int64 {
  34. if i := strings.Index(date, "T"); i > 0 {
  35. return now.MustParse(date[:i]).Unix()
  36. }
  37. return 0
  38. }
  39. func GetYearDay2(tm *time.Time) int {
  40. return tm.Year()*1000 + tm.YearDay()
  41. }