Golang : Convert date string to variants of time.Time type examples




Here are few examples on how to convert a given date of string type to equivalent time.Time type. Most newbies have difficulty in converting a given date of type string into time.Time type and preserve(or change) the original format at the end of the conversion.

time.Parse() function that most Golang developers use to convert a string into time.Time type will pad the output with extra information such as timezone, minutes and second that some developers do not want in the final result. Instead of breaking the year, day and month out and recombine them again, this can be solved easily with the time.Format() function.


1.Convert date string "2016-09-01"

 package main

 import (
 "fmt"
 "os"
 "time"
 )

 func main() {

 dateString := "2016-09-01"

 fmt.Printf("Input : %s\n", dateString)

 //convert string to time.Time type
 layOut := "2006-01-02"
 dateStamp, err := time.Parse(layOut, dateString)

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

 // we want same format as the dateString
 // but time.Parse function provides extra information such as time zone, minutes
 // that we don't need.

 fmt.Printf("Output(local date) : %s\n", dateStamp.Local())
 fmt.Printf("Output(UTC) : %s\n", dateStamp)

 // additional step to format it as input

 convertedDateString := dateStamp.Format(layOut)

 fmt.Printf("Final output : %s\n", convertedDateString)

 }

Output:

Input : 2016-09-01

Output(local date) : 2016-09-01 08:00:00 +0800 SGT

Output(UTC) : 2016-09-01 00:00:00 +0000 UTC

Final output : 2016-09-01


2.Convert date string "2016-09-01" to "dd-MMM-yyyy" or "1-Sep-2016" or "1-September-2016"

 package main

 import (
 "fmt"
 "os"
 "time"
 )

 func main() {

 dateString := "2016-09-01"

 fmt.Printf("Input : %s\n", dateString)

 //convert string to time.Time type
 layOut := "2006-01-02" // yyyy-dd-MM
 dateStamp, err := time.Parse(layOut, dateString)

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

 // we want same format as the dateString
 // but time.Parse function provides extra information such as time zone, minutes
 // that we don't need.

 fmt.Printf("Output(local date) : %s\n", dateStamp.Local())
 fmt.Printf("Output(UTC) : %s\n", dateStamp)

 // additional step to format to dd-MMM-yyyy
 convertedDateString := dateStamp.Format("2-Jan-2006")
 fmt.Printf("Output : %s\n", convertedDateString)

 // or if you prefer the full month name
 fmt.Printf("Full output : %s\n", dateStamp.Format("2-January-2006"))

 }

Input : 2016-09-01

Output(local date) : 2016-09-01 08:00:00 +0800 SGT

Output(UTC) : 2016-09-01 00:00:00 +0000 UTC

Output : 1-Sep-2016

Full output : 1-September-2016


3.Convert date string "2016-09-01" to "E, MMM d yyyy"

 package main

 import (
 "fmt"
 "os"
 "time"
 )

 func main() {

 dateString := "2016-09-01"

 fmt.Printf("Input : %s\n", dateString)

 //convert string to time.Time type
 layOut := "2006-01-02" // yyyy-dd-MM
 dateStamp, err := time.Parse(layOut, dateString)

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

 // format to E, MMM d yyyy
 convertedDateString := dateStamp.Format("Mon 2-Jan-2006")
 fmt.Printf("Output : %s\n", convertedDateString)

 fmt.Printf("Full output : %s\n", dateStamp.Format("Monday 2-January-2006"))

 }

Input : 2016-09-01

Output : Thu 1-Sep-2016

Full output : Thursday 1-September-2016


Hope this helps!

References:

https://www.socketloop.com/tutorials/golang-change-date-format-to-yyyy-mm-dd

https://golang.org/pkg/time/#example_Month

https://www.socketloop.com/tutorials/golang-date-and-time-formatting

  See also : Golang : Convert date or time stamp from string to time.Time type





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