Parsing the string into date
The same concept as the one used in date formatting is also used by date parsing. The same reference date and layout principles can be used. This recipe will show you how to transform the string input into a Time
instance.
How to do it...
- Open the console and create the folder
chapter04/recipe03
. - Navigate to the directory.
- Create the
parse.go
file with the following content:
package main import ( "fmt" "time" ) func main() { // If timezone is not defined // than Parse function returns // the time in UTC timezone. t, err := time.Parse("2/1/2006", "31/7/2015") if err != nil { panic(err) } fmt.Println(t) // If timezone is given than it is parsed // in given timezone t, err = time.Parse("2/1/2006 3:04 PM MST", "31/7/2015 1:25 AM DST") if err != nil { ...