Golang : Get timezone offset from date or timestamp
Problem :
You have a date or time stamp and you want get the time zone offset from the date.
Solution :
First, convert the date or time stamp to string. Next,break each part down with strings.Fields()
function and put the parts into an array.
For example :
package main
import (
"fmt"
"strings"
"time"
)
func main() {
now := time.Now()
fmt.Println(now)
// convert date time to string
// and then to array with strings.Fields() function
dateArray := strings.Fields(now.String())
// get the offset and timezone
fmt.Println(dateArray)
fmt.Println("Date : ", dateArray[0])
fmt.Println("Time : ", dateArray[1])
fmt.Println("Offset : ", dateArray[2])
fmt.Println("TimeZone : ", dateArray[3])
}
Output :
Date : 2015-08-06
Time : 23:44:34.949427127
Offset : -0400
TimeZone : EDT
References :
https://www.socketloop.com/tutorials/golang-convert-unix-timestamp-to-utc-timestamp
See also : Golang : How to get year, month and day?
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
+14.8k Golang : GUI with Qt and OpenCV to capture image from camera
+11.1k Golang : Create S3 bucket with official aws-sdk-go package
+21.9k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+24.2k Golang : Upload to S3 with official aws-sdk-go package
+12.5k Golang : Search and extract certain XML data example
+23.3k Golang : Print out struct values in string format
+28.3k Golang : Connect to database (MySQL/MariaDB) server
+9.5k Golang : Web(Javascript) to server-side websocket example
+10.3k Golang : Text file editor (accept input from screen and save to file)
+11.3k Golang : How to determine a prime number?
+29.3k Golang : missing Git command
+9k Golang : Accept any number of function arguments with three dots(...)