Tran Hau 2020-05-11 23:28:10 +07:00
parent 849062446b
commit 41331c7afd
2 changed files with 44 additions and 0 deletions

44
Development/go/time.md Normal file
View File

@ -0,0 +1,44 @@
# [time](https://golang.org/pkg/time/)
To parse string to time in golang, must define layout first.
Read default layouts in [Constants](https://golang.org/pkg/time/#pkg-constants).
The reference time:
```txt
Mon Jan 2 15:04:05 MST 2006
```
To define your layout, rewrite reference time in your layout.
Example `Parse`:
```go
layout := "2006-01-02 15:04:05"
t, err := time.Parse(layout, "2020-06-09 23:04:02")
if err != nil {
log.Fatal(err)
}
fmt.Println(t)
```
Example `ParseInLocation`:
```go
layout := "2006-01-02 15:04:05"
loc, err := time.LoadLocation("Asia/Ho_Chi_Minh")
if err != nil {
log.Fatal(err)
}
t, err := time.ParseInLocation(layout, "2020-06-09 23:04:02", loc)
if err != nil {
log.Fatal(err)
}
fmt.Println(t)
```