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
+46.5k Golang : Read tab delimited file with encoding/csv package
+32k Golang : How to convert(cast) string to IP address?
+14k Golang : Convert spaces to tabs and back to spaces example
+10.9k Golang : Resolve domain name to IP4 and IP6 addresses.
+9.7k Golang : Extract or copy items from map based on value
+6.8k Golang : Warp text string by number of characters or runes example
+5.5k Golang : Generate Interleaved 2 inch by 5 inch barcode
+7.7k SSL : How to check if current certificate is sha1 or sha2 from command line
+10.1k Golang : Translate language with language package example
+9.3k Golang : Serving HTTP and Websocket from different ports in a program example
+43.7k Golang : Convert []byte to image
+15k Golang : Find commonalities in two slices or arrays example