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
+10.3k Golang : Convert file unix timestamp to UTC time example
+9.1k Golang : How to capture return values from goroutines?
+9k Golang : Capture text return from exec function example
+8.3k Golang : Oanda bot with Telegram and RSI example
+11.5k Use systeminfo to find out installed Windows Hotfix(s) or updates
+12.3k Golang : Validate email address
+13.4k Golang : Get constant name from value
+27.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+6k Fontello : How to load and use fonts?
+7k Golang : Levenshtein distance example
+20.8k Golang : Convert date string to variants of time.Time type examples
+22.7k Golang : Round float to precision example