package main
import (
"fmt"
"time"
)
func main() {
test, err := time.Parse("2006-01-02", "2013-01-12")
if err != nil {
panic(err)
}
fmt.Println(test)
}
[ `run` | done: 4.533945ms ]
2013-01-12 00:00:00 +0000 UTC
其中time.parse格式改变后,未发生变化
test, err := time.Parse("2006/01/02", "2013/01/12")
[ `run` | done: 1.499388ms ]
2013-01-12 00:00:00 +0000 UTC
再换,结果未变:
test, err := time.Parse("01/02/2006", "01/12/2013")
[ `run` | done: 1.471152ms ]
2013-01-12 00:00:00 +0000 UTC
截取取小时:
test, err := time.Parse("2006-01-02 15", "2013-05-13 13")
截取到分钟:
test, err := time.Parse("2006-01-02 15:04", "2013-05-13 10:30")
[ `run` | done: 1.414665ms ]
2013-05-13 10:30:00 +0000 UTC
截取到秒:
test, err := time.Parse("2006-01-02 15:04:05", "2013-05-13 10:30:03")
[ `run` | done: 1.554238ms ]
2013-05-13 10:30:03 +0000 UTC
随着截取位置的不同,通过补零设置