常用类型及数据操作方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

3 years ago
  1. package helper
  2. import (
  3. "strconv"
  4. "time"
  5. )
  6. func FormatDateTime(str string) string {
  7. date, err := strconv.ParseInt(str, 10, 64)
  8. if err != nil {
  9. return ""
  10. }
  11. return time.Unix(date, 0).Format("2006-01-02 15:04:05")
  12. }
  13. func FormatDate(str string) string {
  14. date, err := strconv.ParseInt(str, 10, 64)
  15. if err != nil {
  16. return ""
  17. }
  18. return time.Unix(date, 0).Format("2006-01-02")
  19. }
  20. //获取当天开始时间戳
  21. func GetTodayStartTimeStamp() int64{
  22. var reserveTime time.Time
  23. loc, _ := time.LoadLocation("Asia/Shanghai")
  24. date := time.Now().Format("2006-01-02")
  25. reserveTime,_ = time.ParseInLocation("2006-01-02",date,loc)
  26. return reserveTime.Unix()
  27. }
  28. //获取本周开始时间戳
  29. func GetWeekStartTimeStamp() int64{
  30. var reserveTime time.Time
  31. now := time.Now()
  32. loc, _ := time.LoadLocation("Asia/Shanghai")
  33. offset := int(time.Monday - now.Weekday())
  34. if offset > 0 {
  35. offset = -6
  36. }
  37. weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  38. weekMonday := weekStartDate.Format("2006-01-02")
  39. reserveTime,_ = time.ParseInLocation("2006-01-02",weekMonday,loc)
  40. return reserveTime.Unix()
  41. }
  42. //获取当月开始时间戳
  43. func GetMonthStartTimeStamp() int64{
  44. var reserveTime time.Time
  45. loc, _ := time.LoadLocation("Asia/Shanghai")
  46. date := time.Now().Format("2006-01")
  47. reserveTime,_ = time.ParseInLocation("2006-01",date,loc)
  48. return reserveTime.Unix()
  49. }