package main
import (
"fmt"
"time"
)
func main() {
layout := "2006-01-02 15:04:05"
str := "2022-09-05T12:03:15Z"
t := DateTimeStringFormat(str, layout)
fmt.Println(t)
}
// DateTimeStringFormat 工夫格式化 字符串格式化----后果字符串
func DateTimeStringFormat(timeValue string, fmt string) string {
timeLayout := "2006-01-02T15:04:05.999999999Z07:00" //所需模板
loc, _ := time.LoadLocation("Local") //***获取时区***
theTime, _ := time.ParseInLocation(timeLayout, timeValue, loc) //应用模板在对应时区转化为time.time类型
// 0001-01-01T00:00:00Z这里也示意工夫为null
if theTime.IsZero() {
return ""
} else {
//工夫戳转日期
//dataTimeStr := theTime.Format("2006-01-02 15:04:05") //应用模板格式化为日期字符串
dataTimeStr := theTime.Format(fmt) //应用模板格式化为日期字符串
return dataTimeStr
}
}
发表回复