Home > Backend Development > Golang > How to Convert 'YYYY-MM-DD HH:mm:ss' to 'Mon DD 'YY at HH:mm' in Go?

How to Convert 'YYYY-MM-DD HH:mm:ss' to 'Mon DD 'YY at HH:mm' in Go?

Barbara Streisand
Release: 2024-12-05 01:04:10
Original
571 people have browsed it

How to Convert

Converting Date Formats in Go

Question:

How can I convert a date in the format "2010-01-23 11:44:20" to "Jan 23 '10 at 11:44" using Go?

Answer:

To convert between different date formats in Go, you can utilize the Parse and Format functions from the time package. Both functions accept a reference time (in the desired format) as a parameter, simplifying the formatting process.

Example:

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

This code will convert the original date string to the desired "Jan 23 '10 at 11:44" format using the following steps:

  1. Parse the original date string "2010-01-23 11:44:20" into a time.Time object using time.Parse() with the appropriate reference format "2006-01-02 15:04:05".
  2. Use the Format() function on the time.Time object to convert the date to the new "Jan 2 '06 at 15:04" format.

The above is the detailed content of How to Convert 'YYYY-MM-DD HH:mm:ss' to 'Mon DD 'YY at HH:mm' 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