How to Get Current Timestamp in Golang

To get the current timestamp in Golang, you can use thetime.Now().Unix()” function. The time.Unix() function is a method of the time.Time type in Go, which represents a time value.

The time.Unix() function returns the Unix time, which is the number of seconds elapsed since January 1, 1970, UTC, and the time.Now() function returns a time.Time type, which contains both the date and time.

Example

package main

import (
  "fmt"
  "time"
)

func main() {
  timestamp := time.Now().Unix()
  fmt.Println("The current timestamp is:", timestamp)
}

Output

The current timestamp is: 1676117332

You can see that we got the current timestamp in seconds using the time.Now().Unix() function.

How to get the current time in Go

To get the current time in Golang, you can use the time.Now() function like this: time.Now().

package main

import (
  "fmt"
  "time"
)

func main() {
  tme := time.Now()
  fmt.Println("The current time is:", tme)
}

Output

The current time is: 2023-02-11 17:48:33.002301 +0530 IST m=+0.000221043

That’s it.

Leave a Comment