159

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.

akki
  • 2,021
  • 1
  • 24
  • 35
brianoh
  • 3,407
  • 6
  • 23
  • 23

9 Answers9

176

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.

Dave C
  • 7,729
  • 4
  • 49
  • 65
nmichaels
  • 49,466
  • 12
  • 107
  • 135
  • 19
    I definitely like the `Kitchen` constant (`= "3:04PM"`) – tux21b May 04 '11 at 15:25
  • 8
    Thanks for that info. How does the time package know from passing "20060102150405", just what we are passing, as it is not one of the pre-defined constants in the time package? What is the significance of that date and time in the time package (20060102150405)? It seems a little bit quirky to me, but as long as it works I suppose it doesn't matter as long as we don't make an error when coding it. I guess they didn't see fit to provide a constant for that format, and match the string pattern. – brianoh May 05 '11 at 03:20
  • 11
    @brianoh: See http://golang.org/pkg/time/#Constants It is the time "01/02 03:04:05PM '06 -0700" Because each component has a different number (1, 2, 3, etc.), it can determine from the numbers what components you want. – newacct May 05 '11 at 06:38
  • 1
    Please note that time.LocalTime() doesn't exist anymore : see my answer below to be Go 1.0.3 compatible – Deleplace Jan 16 '13 at 14:38
  • It's very strange that I have to remember a number that much less than my wife's birthday! Do you think GO constant is much important than your families' birthday? why not just '%Y-%m-%d %H:%M:%S‘ ? – Siwei Sep 10 '21 at 03:40
86

All the other response are very miss-leading for somebody coming from google and looking for "timestamp in go"! YYYYMMDDhhmmss is not a "timestamp".

To get the "timestamp" of a date in go (number of seconds from january 1970), the correct function is Time.Unix(), and it really return an integer

Bactisme
  • 1,644
  • 2
  • 15
  • 16
  • 9
    I agree; the question should be titled "current date" not "current timestamp" – brettwhiteman Aug 12 '16 at 05:40
  • 1
    Although it is late, i think .Unix() should point to https://golang.org/pkg/time/#Time.Unix whose return type is int64. – bornfree Feb 11 '18 at 15:39
  • While you are technically correct, I think a lot of coders these days conflate timestamp with, 'The current time in some sort of formatted fashion', so that title would be equally confusing to most. – Roger Hill Jan 30 '19 at 00:46
  • 1
    I have suggested an edit - `Get current time as formatted string in Go?`. Hopefully someone will approve it. – akki Nov 27 '19 at 07:42
76

For readability, best to use the RFC constants in the time package (me thinks)

import "fmt" 
import "time"

func main() {
    fmt.Println(time.Now().Format(time.RFC850))
}
matthewmcneely
  • 979
  • 7
  • 4
  • 4
    How does this produce YYYYMMDDhhmmss ? – Rod May 26 '16 at 06:30
  • 4
    RFC850 produces `Tuesday, 10-Nov-09 23:00:00 UTC` `RFC3339 = "2006-01-02T15:04:05Z07:00"` https://play.golang.org/p/XmobwWSz5pN https://golang.org/pkg/time/ – Chris McKee Oct 02 '19 at 12:19
  • 3
    I personally prefer: `time.Now().Format(time.RFC3339)`, sample output: `2021-08-19T13:52:51+03:00` – Eric Aug 19 '21 at 05:54
36

Use the time.Now() and time.Format() functions (as time.LocalTime() doesn't exist anymore as of Go 1.0.3)

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

Online demo (with date fixed in the past in the playground, never mind)

Deleplace
  • 6,812
  • 5
  • 28
  • 41
  • 7
    And you can use it simply as a string like this: ```s := "Actual time is: "+time.Now().String()``` – Michael Mar 09 '14 at 11:19
11

Find more info in this post: Get current date and time in various format in golang

This is a taste of the different formats that you'll find:

package main

import (
    "fmt"
    "time"
)

func main() {
    currentTime := time.Now()
    fmt.Println("Current Time in String: ", currentTime.String())
    fmt.Println("MM-DD-YYYY : ", currentTime.Format("01-02-2006"))
    fmt.Println("YYYY-MM-DD : ", currentTime.Format("2006-01-02"))
    fmt.Println("YYYY.MM.DD : ", currentTime.Format("2006.01.02 15:04:05"))
    fmt.Println("YYYY#MM#DD {Special Character} : ", currentTime.Format("2006#01#02"))
    fmt.Println("YYYY-MM-DD hh:mm:ss : ", currentTime.Format("2006-01-02 15:04:05"))
    fmt.Println("Time with MicroSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000"))
    fmt.Println("Time with NanoSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000000"))
    fmt.Println("ShortNum Month : ", currentTime.Format("2006-1-02"))
    fmt.Println("LongMonth : ", currentTime.Format("2006-January-02"))
    fmt.Println("ShortMonth : ", currentTime.Format("2006-Jan-02"))
    fmt.Println("ShortYear : ", currentTime.Format("06-Jan-02"))
    fmt.Println("LongWeekDay : ", currentTime.Format("2006-01-02 15:04:05 Monday"))
    fmt.Println("ShortWeek Day : ", currentTime.Format("2006-01-02 Mon"))
    fmt.Println("ShortDay : ", currentTime.Format("Mon 2006-01-2"))
    fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5"))
    fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 PM"))
    fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 pm"))
}

The output is:

Current Time in String:  2017-07-04 00:47:20.1424751 +0530 IST
MM-DD-YYYY :  07-04-2017
YYYY-MM-DD :  2017-07-04
YYYY.MM.DD :  2017.07.04 00:47:20
YYYY#MM#DD {Special Character} :  2017#07#04
YYYY-MM-DD hh:mm:ss :  2017-07-04 00:47:20
Time with MicroSeconds:  2017-07-04 00:47:20.142475
Time with NanoSeconds:  2017-07-04 00:47:20.142475100
ShortNum Month :  2017-7-04
LongMonth :  2017-July-04
ShortMonth :  2017-Jul-04
ShortYear :  17-Jul-04
LongWeekDay :  2017-07-04 00:47:20 Tuesday
ShortWeek Day :  2017-07-04 Tue
ShortDay :  Tue 2017-07-4
Short Hour Minute Second:  2017-07-04 12:47:20
Short Hour Minute Second:  2017-07-04 12:47:20 AM
Short Hour Minute Second:  2017-07-04 12:47:20 am
Maëlan
  • 3,586
  • 1
  • 15
  • 35
3

https://golang.org/src/time/format.go specified For parsing time 15 is used for Hours, 04 is used for minutes, 05 for seconds.

For parsing Date 11, Jan, January is for months, 02, Mon, Monday for Day of the month, 2006 for year and of course MST for zone

But you can use this layout as well, which I find very simple. "Mon Jan 2 15:04:05 MST 2006"

    const layout = "Mon Jan 2 15:04:05 MST 2006"
    userTimeString := "Fri Dec 6 13:05:05 CET 2019"

    t, _ := time.Parse(layout, userTimeString)
    fmt.Println("Server: ", t.Format(time.RFC850))
    //Server:  Friday, 06-Dec-19 13:05:05 CET

    mumbai, _ := time.LoadLocation("Asia/Kolkata")
    mumbaiTime := t.In(mumbai)
    fmt.Println("Mumbai: ", mumbaiTime.Format(time.RFC850))
    //Mumbai:  Friday, 06-Dec-19 18:35:05 IST

DEMO

STEEL
  • 8,955
  • 9
  • 67
  • 89
2

You can simply use like strconv.Itoa(int(time.Now().Unix()))

pallabi
  • 21
  • 2
1

As an echo to @Bactisme's response, the way one would go about retrieving the current timestamp (in milliseconds, for example) is:

msec := time.Now().UnixNano() / 1000000

Resource: https://gobyexample.com/epoch

Jack Ryan
  • 1,287
  • 12
  • 26
-4

To answer the exact question:

import "github.com/golang/protobuf/ptypes"

Timestamp, _ = ptypes.TimestampProto(time.Now())
suran
  • 39