Home  >  Article  >  Backend Development  >  golang date type conversion

golang date type conversion

WBOY
WBOYOriginal
2023-05-10 10:02:361653browse

Golang is a very popular programming language that is widely recognized for its efficiency and simplicity. In developing applications, it is often necessary to convert date types. Let's introduce the method of date type conversion in Golang.

In Golang, there are two main date types: time.Time and string. The time.Time type can represent a point in time, while the string type is a string type representation. To convert between two types, you can use some functions provided in Golang.

  1. String to time.Time

In Golang, you can convert a string type date to time.Time type through the time.Parse() function. An example is as follows:

str := "2021-06-01 12:00:00"
layout := "2006-01-02 15:04:05"
t, err := time.Parse(layout, str)
if err != nil {
    fmt.Println(err)
}
fmt.Println(t)

The parsing function time.Parse() needs to pass two parameters: one is the date string, and the other is the format of the date string. In the above example, the date string is 2021-06-01 12:00:00 and the format string is 2006-01-02 15:04:05. What needs to be noted here is that the numbers in the format string must be arranged in the order of year, month, day, hour, minute, and second, and the corresponding format characters must be used.

  1. time.Time to string

In Golang, a time.Time type date can be converted to string type through the time.Format() function. An example is as follows:

t := time.Now()
layout := "2006-01-02 15:04:05"
str := t.Format(layout)
fmt.Println(str)

The formatting function time.Format() needs to pass a format string, which is the same as the string format in the parsing function. In the above example, the time.Now() function is used to obtain the current time, and the format string is 2006-01-02 15:04:05.

  1. Unix timestamp to time.Time

In Golang, you can convert a Unix timestamp to time.Time type through the time.Unix() function. An example is as follows:

unixTime := int64(1622496000)
t := time.Unix(unixTime, 0)
fmt.Println(t)

The function time.Unix() needs to pass two parameters: one is the Unix timestamp, and the other is the nanosecond offset. Here, 0 is used as the offset.

  1. time.Time to Unix timestamp

In Golang, you can convert a time.Time type into a Unix timestamp through the time.Unix() function. An example is as follows:

t := time.Now()
unixTime := t.Unix()
fmt.Println(unixTime)

The function time.Unix() will return a Unix timestamp, and the timestamp is of type int64. In the above example, the time.Now() function is used to obtain the current time.

Summary

Golang date type conversion is not difficult, you just need to use the appropriate function. The four methods introduced above can meet most date type conversion needs. Of course, there are many special cases that need to be considered in practical applications and need to be adjusted according to the specific situation, but this is enough to get us started.

The above is the detailed content of golang date type conversion. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn