Golang time.Time.Zone() function example

package time

Golang time.Time.Zone() function usage example.

 package main

 import (
 "fmt"
 "time"
 )

 func main() {

 now := time.Now()

 fmt.Println("Now: ", now)

 // reduce the date format
 // remember NOT to use 2006-01-01 or 02-02 or same digit
 // for month and date. Will cause weird date result
 fmt.Println(now.Format("2006-01-02"))

 fmt.Println("Location: ", now.Location())

 // get the time zone name
 z, offset := now.Zone()

 fmt.Printf("Location(Time Zone) : %v and offset : %v \n", z, offset)
 }

Sample output :

Now: 2015-08-11 17:21:52.776537624 +0800 MYT

2015-08-11

Location: Local

Location(Time Zone) : MYT and offset : 28800

Reference :

http://golang.org/pkg/time/#Time.Zone

  See also : Golang time.Location type and LoadLocation() function example

Advertisement