Golang time.Parse() function example

package time

Golang time.Parse() function usage example

 package main

 import (
  "fmt"
  "time"
 )

 func main() {

  // get current timestamp
  currentTime := time.Now().Local()

  fmt.Println(currentTime)

  // don't like the standard format ?
  // change it

  newFormat := currentTime.Format("2006-01-02 15:00:00 +0800")
  fmt.Println(newFormat)

  fmt.Println("===============================")

  // Changing time layout(form)
  str := "Dec 29, 2014 at 7:54pm (SGT)"
  newLayout := "Jan 2, 2006 at 3:04pm (MST)"
  newTime, _ := time.Parse(newLayout, str)
  fmt.Println("1 : ", newTime)

  shortForm := "2006-Jan-02 (SGT)"
  str = "2014-Dec-29 (SGT)"
  newTime, _ = time.Parse(shortForm, str)
  fmt.Println("2 : ",newTime)

 }

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

2009-11-10 23:00:00 +0800

===============================

1 : 2014-12-29 19:54:00 +0000 SGT

2 : 2014-12-29 00:00:00 +0000 UTC

Reference :

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

Advertisement