package main
import (
"fmt"
"time"
)
const (
// 2006-01-02 15:04:05为go的标准格式
timeFormat1 = "2006-01-02 15:04:05"
timeFormat2 = "2006-01-02-15-04-05"
timeFormat3 = "2006-01-02 15:04:05.000" // 可以精确到毫秒ms
// 非go的标准格式 打印的时间格式为未知时间
timeFormat4 = "2022-01-02 10:04:05"
timeFormat5 = "2022-01-02-10-04-05"
timeFormat6 = "2022-01-02 10:04:05.000" // 可以精确到毫秒ms
)
func main() {
// 获取当前时间
now := time.Now()
fmt.Printf("timeFormat1[%s] now[%s]\n", timeFormat1, now.Format(timeFormat1))
fmt.Printf("timeFormat2[%s] now[%s]\n", timeFormat2, now.Format(timeFormat2))
fmt.Printf("timeFormat3[%s] now[%s]\n", timeFormat3, now.Format(timeFormat3))
fmt.Printf("timeFormat4[%s] now[%s]\n", timeFormat4, now.Format(timeFormat4))
fmt.Printf("timeFormat5[%s] now[%s]\n", timeFormat5, now.Format(timeFormat5))
fmt.Printf("timeFormat6[%s] now[%s]\n", timeFormat6, now.Format(timeFormat6))
}
运行结果如下:
timeFormat1[2006-01-02 15:04:05] now[2022-06-09 09:55:48]
timeFormat2[2006-01-02-15-04-05] now[2022-06-09-09-55-48]
timeFormat3[2006-01-02 15:04:05.000] now[2022-06-09 09:55:48.691]
#非go标准格式输出的时间不符合预期
timeFormat4[2022-01-02 10:04:05] now[9099-06-09 60:55:48]
timeFormat5[2022-01-02-10-04-05] now[9099-06-09-60-55-48]
timeFormat6[2022-01-02 10:04:05.000] now[9099-06-09 60:55:48.691]
注意:时间格式字符串一定得是go的诞生时间"2006-01-02 15:04:05",否则时间输出会超出你的想象。。。。