In the Go language, we can easily convert strings into time types, and it also supports conversion of multiple time formats. This article will introduce how to use Go language to convert a string into a time type.
1. Time formatting
Before performing time conversion, we need to first understand how to format time. In the Go language, time formatting is defined using time templates. "Mon", "Jan", "2", "15:04:05", "MST", "2006", etc. in the time template all represent specific The time contents have specific meanings in the time template. The following are some commonly used time templates:
时间模板 描述 Mon 月份简写 January 月份全称 02 月份中的日,带前导零 2 月份中的日,不带前导零 15 小时(24小时制),带前导零 3:04 PM 小时(12小时制) 04 小时中的分钟,带前导零 5 小时中的分钟,不带前导零 05.000 带秒的小数点 PM 上午/下午标识符 MST 时区缩写 2006 年份,用于参考 06 年份最后两位,用于参考 01 相对于年份的月份,带前导零 Jan 相对于年份的月份,不带前导零 02 相对于月份的日,带前导零
Through the above time templates, we can define the time format we need.
package main import ( "fmt" "time" ) func main() { t := time.Now() fmt.Println(t.Format("2006-01-02 15:04:05")) }
In the above code, we use the time template "2006-01-02 15:04:05" to format the time, and the final output result is "2022-05-17 13:23:51".
2. Convert string to time
In Go language, we can use the Parse method in the time package to convert a string into a time type. The Parse method requires two parameters. The first parameter is a time string, and the second parameter is the format of the time string. Both parameters are string types.
package main import ( "fmt" "time" ) func main() { str := "2022-05-17 13:23:51" layout := "2006-01-02 15:04:05" t, _ := time.Parse(layout, str) fmt.Println(t) }
In the above code, we passed the time string "2022-05-17 13:23:51" and the time template "2006-01-02 15:04:05" into the time.Parse method , and finally the converted time is output through fmt.Println.
3. Multiple time format conversion
In practical applications, we are likely to encounter multiple different time formats. At this time, we need to support multiple time format conversions. Go language provides the time.ParseInLocation method to support multiple time format conversions. The ParseInLocation method requires three parameters. The first parameter is a time string, the second parameter is a time template, and the third parameter is a specified time zone.
package main import ( "fmt" "time" ) func main() { str1 := "2022-05-17 13:23:51" layout1 := "2006-01-02 15:04:05" str2 := "2019/01/01 12:00:00" layout2 := "2006/01/02 15:04:05" loc, _ := time.LoadLocation("Asia/Shanghai") t1, _ := time.ParseInLocation(layout1, str1, loc) fmt.Println(t1) t2, _ := time.ParseInLocation(layout2, str2, loc) fmt.Println(t2) }
In the above code, we use the time.ParseInLocation method to support two different time format conversions, where str1 and layout1 represent the first time format, and str2 and layout2 represent the second time format. Specify the time zone as "Asia/Shanghai" through the LoadLocation method, and finally output the time in two different formats through fmt.Println.
4. Summary
In the Go language, we can convert strings into time types through the time.Parse and time.ParseInLocation methods, and support multiple time format conversions. When we need to convert time types, we can use the above method and use an appropriate time template for time formatting.
The above is the detailed content of golang character to time. For more information, please follow other related articles on the PHP Chinese website!