Golang time.Location type and LoadLocation() function example

package time

Golang time.Location type and LoadLocation() function usage example.

 package main

 import (
  "fmt"
  "time"
 )

 func main() {

  now := time.Now()

  fmt.Println("Now: ", now)

  fmt.Println(now.Format("Mon, Jan 2, 2006 at 3:04pm"))

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

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

  fmt.Println("Location(Time Zone): ", z)

  // load different time zone
  est, err := time.LoadLocation("EST") //<---------------------- here !

  if err != nil {
 fmt.Println(err)
  }

  fmt.Println("Load Location : ", est)

  dayInEST := time.Date(2015, 18, 5, 12, 15, 0, 0, est)

  fmt.Println("This code is created on : ", dayInEST.Format("Monday"))

 }

Output :

Now: 2009-11-10 23:00:00 +0000 UTC

Tue, Nov 10, 2009 at 11:00pm

Location: UTC

Location(Time Zone): UTC

Load Location : EST

This code is created on : Sunday

References :

http://golang.org/pkg/time/#LoadLocation

http://golang.org/pkg/time/#Location

Advertisement