In software development, processing time is a recurring issue. Especially in large systems, time is an essential part of recording events, scheduling tasks, and analyzing data. Therefore, using a suitable time processing library becomes a very important task. This article will introduce the use of time processing library time
in Go language.
In the Go language, we can use the time.Time
type to represent time. A value of type Time
contains year, month, day, hour, minute, second, nanosecond and time zone information. This type is built-in so we don't need to install any additional libraries to use it.
The method to get the current local time is to use the time.Now()
function. It will return a time object of type time.Time
, which represents the current time when the program is running.
func main() { current_time := time.Now() fmt.Println(current_time) }
Output results:
2021-05-17 16:34:22.7241986 +0800 CST m=+0.000731901
It is a very common requirement to display time in different formats. In the Go language, we can use the time.Format()
function to format time. This function receives a format string and converts the time object into the corresponding string according to this format.
func main() { current_time := time.Now() formatted_time := current_time.Format("2006-01-02 15:04:05") fmt.Println(formatted_time) }
Output result:
2021-05-17 16:34:22
In the format string, several commonly used placeholders are as follows:
Placeholder The symbol | means |
---|---|
2006 | is fixed to a 4-digit year, indicating a Standard |
01 | is fixed to a 2-digit month. If there are less than 2 digits, add 0 |
##02
| is fixed as a 2-digit date. If there are less than 2 digits, add 0|
15
| Fixed to 2 digits of hour, 24-hour system, if there are less than two digits, add 0 on the left side |
04
| Fixed to 2 For minutes with 2 digits, add 0 to the left if there are less than two digits.|
05
|
Increase or decrease the duration
time.Add()The method allows us to add or reduce a period of time:<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:go;toolbar:false;'>func main() {
current_time := time.Now()
after_one_hour := current_time.Add(time.Hour)
fmt.Println(after_one_hour)
}</pre><div class="contentsignin">Copy after login</div></div>
Output result:
2021-05-17 17:34:22.6523704 +0800 CST m=+3601.928903701
In this example, we use
time.Hour to represent the length of one hour, and then use the Add()
method to add the current time to this length of time. The return value of this method is a time object, which represents the time point one hour after the current time. We can also use the
method to calculate the time difference between two times: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:go;toolbar:false;'>func main() {
start_time := time.Now()
end_time := time.Now().Add(time.Hour)
duration := end_time.Sub(start_time)
fmt.Println(duration)
}</pre><div class="contentsignin">Copy after login</div></div>
Output result:
1h0m0s
here The calculation result is a value of type
time.Duration, which represents the length of time between two time points. Set time
method allows us to create a time object based on the specified year, month, day, hour, minute, second and time zone: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:go;toolbar:false;'>func main() {
t := time.Date(2021, 5, 17, 15, 30, 0, 0, time.Local)
fmt.Println(t)
}</pre><div class="contentsignin">Copy after login</div></div>
Output result:
2021-05-17 15:30:00 +0800 CST
Get time information
Time type provides some methods to get time information, such as year, month, day , hours, minutes, seconds, etc. The following are some commonly used methods: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:go;toolbar:false;'>func main() {
current_time := time.Now()
fmt.Println(current_time.Year()) // 获取年份
fmt.Println(current_time.Month()) // 获取月份
fmt.Println(current_time.Day()) // 获取日期
fmt.Println(current_time.Hour()) // 获取小时数
fmt.Println(current_time.Minute()) // 获取分钟数
fmt.Println(current_time.Second()) // 获取秒数
fmt.Println(current_time.Weekday()) // 获取星期几,0表示星期日
}</pre><div class="contentsignin">Copy after login</div></div>
Output results:
2021 May 17 16 34 22 Monday
Reference documentation
[Go language official documentation](https://golang.org/ pkg/time/)The above is the detailed content of Use of time processing library in Go language. For more information, please follow other related articles on the PHP Chinese website!