Trying to parse string date to date in yyyy-mm-dd format

Following is the code segment i am trying :

package main
import (
“fmt”
“time”
)

func main(){
const format = “2005-03-04”
pdate,_ := time.Parse(format, “2015-05-06”)
fmt.Println(pdate)

const shortForm = "2006-01-02"
t, _ := time.Parse(shortForm, "2013-02-03")
fmt.Println(t)

}

Corresponding output is as such:

0001-01-01 00:00:00 +0000 UTC
2013-02-03 00:00:00 +0000 UTC

Why is it happening is a different way though i have used same syntax

1 Like

The documentation of time.Parse says:

Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time, defined to be

Mon Jan 2 15:04:05 -0700 MST 2006

would be interpreted if it were the value; it serves as an example of the input format. The same interpretation will then be made to the input string.

That means you cannot use an arbitrary date like “2005-03-04;" it has to be the “magic” value 2006-01-02 15:04:05.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.