Home > Backend Development > Golang > How Can I Get a Formatted Timestamp in Go?

How Can I Get a Formatted Timestamp in Go?

Patricia Arquette
Release: 2024-12-02 16:58:11
Original
699 people have browsed it

How Can I Get a Formatted Timestamp in Go?

Obtaining Formatted Timestamps in Go

In Go, retrieving the current timestamp as a formatted string can be achieved using a combination of the time.Now() function and the time.Format() method. This method allows for customizable string representations of date and time.

Solution:

To format the timestamp in the specified format (YYYYMMDDhhmmss), use the以下代码:

t := time.Now()
fmt.Println(t.Format("20060102150405"))
Copy after login

This code captures the current time using time.Now() and applies the "20060102150405" format string to generate the desired timestamp representation. For instance, at Eastern Daylight Time, it might yield "20110504111515."

Additional Considerations:

  • The time package provides predefined time formats, such as RFC1123 or RFC3339, accessible via constants.
  • To acquire the current time in UTC rather than the local time zone, use time.Now().UTC().
  • You can customize the format string as needed to meet your specific requirements. Consult the time package documentation for further options.

The above is the detailed content of How Can I Get a Formatted Timestamp 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