Use time.LoadLocation()
to get location information of particular timezone. Then use the .In()
method of time object, to convert the current time into expected timezone.
Example:
now := time.Now()
fmt.Println("My place", now)
// 2019-07-23 18:14:23.6036439 +0700 +07
locSingapore, _ := time.LoadLocation("Asia/Singapore")
nowInSingapore := now.In(locSingapore)
fmt.Println("Singapore", nowInSingapore)
// 2019-07-23 19:14:23.6036439 +0800
locLondon, _ := time.LoadLocation("Europe/London")
nowInLondon := now.In(locLondon)
fmt.Println("London", nowInLondon)
// 2019-07-23 12:14:23.6036439 +0100 BST
Explanations:
- From code above we can see that
time.Now()
timezone is +7
, it's because I live in West Indonesia.
- But
nowInSingapore
timezone is +8
, it's because the now
object are adjusted into singapore timezone.
- And the last one,
nowInLondon
is showing another different timezone, +1
.
And if we compare all of those time, it's essentially same time.
18:14:23 WIB (GMT +7)
== 19:14:23 GMT +8
== 12:14:23 BST (GMT +1)