HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoCritical

Get current time as formatted string in Go?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
timecurrentgetstringformatted

Problem

What's the best way to get the current timestamp in Go and convert to string? I need both date and time in eg. YYYYMMDDhhmmss format.

Solution

Use the time.Now() function and the time.Format() method.

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


prints out 20110504111515, or at least it did a few minutes ago. (I'm on Eastern Daylight Time.) There are several pre-defined time formats in the constants defined in the time package.

You can use time.Now().UTC() if you'd rather have UTC than your local time zone.

Code Snippets

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

Context

Stack Overflow Q#5885486, score: 190

Revisions (0)

No revisions yet.