Skip to content

Golang: How to Parse Unix Timestamp to time Type

The standard time package in Golang has many helper functions that can help you when you dealing with time conversion.

Unix() function accepts an integer and returns the time:

func Unix(sec int64, nsec int64) Time

Run this example and boom! your time is ready in time.Time format 🙂

package main

import (
	"fmt"
	"log"
	"strconv"
	"time"
)

func main() {
	stringUnix := "1654963822"

	// if your unix timestamp is not in int64 format
	intUnix, err := strconv.ParseInt(stringUnix, 10, 64)
	if err != nil {
		log.Fatal(err)
	}

	// int64 to time.Time
	myTime := time.Unix(intUnix, 0)

	fmt.Println(myTime)
}

Leave a Reply

Your email address will not be published. Required fields are marked *