常用类型及数据操作方法
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.

81 lines
1.8 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. }
  50. // RFC3339ToCSTLayout convert rfc3339 value to china standard time layout
  51. func RFC3339ToCSTLayout(value string) (string, error) {
  52. var (
  53. cst *time.Location
  54. )
  55. var err error
  56. if cst, err = time.LoadLocation("Asia/Shanghai"); err != nil {
  57. panic(err)
  58. }
  59. ts, err := time.Parse(time.RFC3339, value)
  60. if err != nil {
  61. return "", err
  62. }
  63. return ts.In(cst).Format("2006-01-02 15:04:05"), nil
  64. }