// TimeToTimestamp string类型的时间转为时间戳
func TimeToTimestamp(t string) int64 {
if t == "" {
return 0
}
timeLayout := "2006-01-02"
switch len(t) {
case 10:
timeLayout = "2006-01-02"
case 19:
timeLayout = "2006-01-02 15:04:05"
}
loc, _ := time.LoadLocation("Local") //获取时区
tmp, _ := time.ParseInLocation(timeLayout, t, loc)
timestamp := tmp.Unix()
return timestamp
}
// TimestampToTime 时间戳转为string类型的时间, 带时分秒
func TimestampToTime(tmp int64) string {
if tmp == 0 {
return ""
}
timeLayout := "2006-01-02 15:04:05"
t := time.Unix(tmp, 0).Format(timeLayout)
return t
}
Go语言学习笔记 时间转换函数
最新推荐文章于 2022-04-07 12:32:28 发布