convert_datetime.go 674 B

12345678910111213141516171819202122232425262728293031
  1. package gdate
  2. import (
  3. "fmt"
  4. "gbase/glog"
  5. "strconv"
  6. "time"
  7. )
  8. func ConvertTimestampToDatetime(ts string) (date string) {
  9. // 将字符串转换为整数,时间戳单位是毫秒
  10. timestamp, err := strconv.ParseInt(ts, 10, 64)
  11. if err != nil {
  12. glog.XWarning(fmt.Sprintf("strconv.ParseInt ts error : %v", err))
  13. return
  14. }
  15. if len(ts) <= 10 {
  16. timestamp = timestamp * int64(time.Second)
  17. } else {
  18. // 将毫秒时间戳转换为纳秒时间戳
  19. timestamp = timestamp * int64(time.Millisecond)
  20. }
  21. // 将时间戳转换为time.Time类型
  22. t := time.Unix(0, timestamp)
  23. // 格式化时间为字符串
  24. timeStr := t.Format("2006-01-02 15:04:05")
  25. return timeStr
  26. }