要将Go中的日期格式化为YYYY-MM-DD ,使用 time.Format()函数,其布局为:"2006-01-02" 。要将日期格式化为DD/MM/YYYY ,请使用"02/01/2006" 布局。要将日期格式化为YYYY-MM-DD hh:mm:ss ,请使用"2006-01-02 15:04:05" 布局。
实例
Go中的YYY-MM-DD日期格式
package main
import (
"fmt"
"time"
)
const (
YYYYMMDD = "2006-01-02"
)
func main() {
now := time.Now().UTC()
fmt.Println(now.Format(YYYYMMDD))
}
输出
2022-03-14
Go中的DD/MM/YYY日期格式
package main
import (
"fmt"
"time"
)
const (
DDMMYYYY = "02/01/2006"
)
func main() {
now := time.Now().UTC()
fmt.Println(now.Format(DDMMYYYY))
}
输出
14/03/2022
Go中的YYYY-MM-DD hh:mm:ss日期格式
package main
import (
"fmt"
"time"
)
const (
DDMMYYYYhhmmss = "2006-01-02 15:04:05"
)
func main() {
now := time.Now().UTC()
fmt.Println(now.Format(DDMMYYYYhhmmss))
}
输出
2022-03-14 05:41:33