Home > Article > Backend Development > Share Golang's tips for getting timestamps
Golang timestamp acquisition skills sharing, need specific code examples
In the development process, we often need to obtain the timestamp (timestamp) to represent a point in time, especially It is when dealing with logging, timing and other requirements. Although time operations are relatively simple in Golang, there are still some techniques that can help us obtain timestamps more conveniently. This article will share some techniques for obtaining timestamps in Golang and give specific code examples.
A timestamp is a number used to mark a certain point in time, usually from January 1, 1970 00:00:00 UTC to a certain The number of milliseconds or seconds between times. In Golang, timestamps can be generated and manipulated through the time
package.
First, let’s look at how to get the timestamp of the current time. In Golang, you can use the time.Now()
function to get the current time, and then use the Unix()
or UnixNano()
method to get the corresponding timestamp.
The following is a code example to obtain the current timestamp:
package main import ( "fmt" "time" ) func main() { timestamp := time.Now().Unix() fmt.Println("当前时间戳:", timestamp) }
In the above code, we use time.Now().Unix()
to obtain the current time stamp, and use fmt.Println()
to output the timestamp to the console.
In addition to obtaining the current timestamp, sometimes we also need to format the timestamp into a specified date and time format. In Golang, you can use the Format()
method provided by the time
package to format the output timestamp.
The following is a code example to format the timestamp into a specified format:
package main import ( "fmt" "time" ) func main() { timestamp := time.Now().Unix() tm := time.Unix(timestamp, 0) fmt.Println("当前时间:", tm.Format("2006-01-02 15:04:05")) }
In the above code, we first obtain the current timestamp, and then use time.Unix()
Method converts the timestamp into a time.Time
object, and finally uses the Format()
method to format the time into the specified format.
Sometimes, we may need to convert a time string to a timestamp. In Golang, you can use the Parse()
method provided by the time
package to parse the time string into the corresponding time.Time
object, and then use Unix()
or UnixNano()
method to obtain the corresponding timestamp.
The following is a code example to convert a time string into a timestamp:
package main import ( "fmt" "time" ) func main() { layout := "2006-01-02 15:04:05" str := "2022-01-01 12:34:56" tm, err := time.Parse(layout, str) if err == nil { timestamp := tm.Unix() fmt.Println("时间字符串:", str) fmt.Println("时间戳:", timestamp) } else { fmt.Println("时间字符串解析失败") } }
In the above code, we first define a time string str
and the time character The format of the string layout
, and then use the time.Parse()
method to parse the time string into a time.Time
object. If the parsing is successful, we can use the Unix()
method to get the timestamp.
On the contrary, sometimes we also need to convert a timestamp into a time string. In Golang, you can use the Format()
method provided by the time
package to convert the timestamp into a time string in a specified format.
The following is a code example to convert a timestamp into a time string:
package main import ( "fmt" "time" ) func main() { timestamp := time.Now().Unix() tm := time.Unix(timestamp, 0) layout := "2006-01-02 15:04:05" str := tm.Format(layout) fmt.Println("时间戳:", timestamp) fmt.Println("时间字符串:", str) }
In the above code, we first get the current timestamp and use time.Unix()
Convert it to a time.Time
object. Then, we use the Format()
method to format the time into the specified format and output the result to the console.
This article introduces some techniques for obtaining timestamps in Golang and gives specific code examples. We can obtain the current timestamp, format the output timestamp, and convert time strings to timestamps through the functions and methods provided by the time
package. These techniques can help us handle time-related requirements more conveniently and improve development efficiency. I hope this article will be helpful to your time operations in Golang development.
The above is the detailed content of Share Golang's tips for getting timestamps. For more information, please follow other related articles on the PHP Chinese website!