1. When we use the Go language time and date type, we need to import the time package and the method of importing the package. I won't say more about it.
In the go language, we will use the time.Time type to represent time.
Get the current time, now:= time.Now()
package main
import (
"fmt"
"time"
)
func main() {
now_time:= time.Now()
fmt.Println(now_time)
}
Get the following result
2020-07-05 12:04:21.3247109 +0800 CST m=+0.004010401
Use the following method to get some attributes of the current time
time.Now().year(), get the current time year
time.Now().Month(), get the current time month
time.Now().Day(), get the current time, date and day
time.Now().Hour(), get the current hour time
time.Now().Minute(), get the current time in minutes
package main
import (
"fmt"
"time"
)
func main() {
now_time:= time.Now()
fmt.Println(now_time)
fmt.Println(now_time.Year())
fmt.Println(now_time.Month())
fmt.Println(now_time.Day())
fmt.Println(now_time.Hour())
fmt.Println(now_time.Minute())
}
result:
2020-07-05 12:10:58.6773158 +0800 CST m=+0.006015301
2020
July
5
12
10
time.Duration is used to represent nanoseconds
Time formatting issues:
Here is a special note. In the Go language, the time format conversion must be converted using the birth time of the Go language, otherwise the converted date is incorrect.
The birth of Go language: 2006/01/02 15:04:05
example:
package main
import (
"fmt"
"time"
)
func main() {
now_time:= time.Now()
fmt.Println(now_time.Format("02/1/2006 15:04"))
fmt.Println(now_time.Format("02/1/2006"))
fmt.Println(now_time.Format("2006/1/02 15:04"))
}
The results are as follows:
05/7/2020 12:23
05/7/2020
2020/7/05 12:23
You can see that the above result is the current time we converted from the birth date of the go language. So let's modify the birth time of go language and see what it looks like after conversion.
Error case
################################################################################################
package main
import (
"fmt"
"time"
)
func main() {
now_time:= time.Now()
fmt.Println(now_time.Format("02/1/2007 15:04"))
fmt.Println(now_time.Format("02/5/2006"))
fmt.Println(now_time.Format("2006/1/02 17:04"))
}
The results are as follows:
05/7/5007 12:25
05/35/2020
2020/7/05 77:25
As you can see, the results are all wrong. Here is the place to pay attention.
##############################################################################################