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
+17.2k Golang : Example for RSA package functions
+7.8k Golang : automatically figure out array length(size) with three dots
+6.4k Golang : Individual and total number of words counter example
+15.2k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+13.3k Golang : Get URI segments by number and assign as variable example
+7k Prevent Write failed: Broken pipe problem during ssh session with screen command
+6.9k Golang : Append and add item in slice
+10.1k Golang : Post data with url.Values{}
+7.2k Golang : Ackermann function example
+12.7k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+11.4k Golang : Get terminal width and height example