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
+6k Golang : Get Alexa ranking data example
+9.5k Golang : Get local time and equivalent time in different time zone
+8.2k Golang : Find correlation coefficient example
+5.9k Golang : How to call function inside template with template.FuncMap
+9k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+5.9k Golang : Fibonacci number generator examples
+11.3k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+19.1k Golang : Underscore or snake_case to camel case example
+7.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+7.7k Golang : Get final balance from bit coin address example
+32.1k Golang : Create x509 certificate, private and public keys
+50k Golang : Check if item is in slice/array