Home > Backend Development > Golang > How to Convert Date Formats in Go?

How to Convert Date Formats in Go?

Barbara Streisand
Release: 2024-12-06 13:22:18
Original
438 people have browsed it

How to Convert Date Formats in Go?

Converting Date Formats in Go

Converting dates between different formats is a common task in software development. In Go, the time package provides a range of functions for manipulating dates and times.

Converting to a Specific Format

One question arose from a user who wished to convert a date from the format 2010-01-23 11:44:20 to Jan 23 '10 at 11:44. This conversion involves parsing the original date string and then using the Format function to generate the desired format.

Solution

To solve this problem, you can use the following code:

dtstr1 := "2010-01-23 11:44:20"
dt, _ := time.Parse("2006-01-02 15:04:05", dtstr1)

dtstr2 := dt.Format("Jan 2 '06 at 15:04")
Copy after login

The Parse function takes two parameters: the layout of the input date string and the date string itself. The layout string specifies the format of the input date, and the Parse function returns a Time object representing the date.

The Format function takes two parameters: the layout of the desired output and the Time object. The layout string specifies the format of the output, and the Format function returns the date string in the desired format.

The above is the detailed content of How to Convert Date Formats in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template