Golang : Convert Unix timestamp to UTC timestamp
Problem :
You have a Unix timestamp and you want to convert it to UTC time format. How to do that in Golang?
For example :
>date +%s
1432572732
Solution :
Use the strconv.ParseInt()
function to convert unix time stamp to integer value first and then use time.Unix()
to convert the integer value to proper UTC (Coordinated Universal Time) time stamp.
Here you go:
package main
import (
"fmt"
"os"
"strconv"
"time"
)
func main() {
unixTimeStamp := "1432572732"
unixIntValue, err := strconv.ParseInt(unixTimeStamp, 10, 64)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
timeStamp := time.Unix(unixIntValue, 0)
fmt.Println(timeStamp)
}
Output :
2015-05-25 16:52:12 +0000 UTC
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+4.6k Golang : What is StructTag and how to get StructTag's value?
+11.3k Golang : How to display image file or expose CSS, JS files from localhost?
+17.4k Golang : Example for RSA package functions
+9.5k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+4.8k Golang : Use NLP to get sentences for each paragraph example
+8.7k Golang : Resumable upload to Google Drive(RESTful) example
+7.7k Golang : Set or add headers for many or different handlers
+4.6k Golang : Shortening import identifier
+7.8k Yum Error: no such table: packages
+11.6k Golang : How to calculate the distance between two coordinates using Haversine formula
+18.4k Golang : Convert seconds to human readable time format example
+12.1k Swift : Convert (cast) Int to String ?