If you have a date stored as a string you will need to parse it. Use time.Parse
.
// time.Parse( format , date to parse)
date, err := time.Parse("01/02/2006", "04/08/2017")
if err != nil {
panic(err)
}
fmt.Println(date)
// Prints 2017-04-08 00:00:00 +0000 UTC
The first parameter is the layout in which the string stores the date and the second parameter is the string that contains the date. 01/02/2006
is the same than saying the format is MM/DD/YYYY
.
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.
You can see the constants defined in the time package to know how to write the layout string, but note that the constants are not exported and can't be used outside the time package.
const (
stdLongMonth // "January"
stdMonth // "Jan"
stdNumMonth // "1"
stdZeroMonth // "01"
stdLongWeekDay // "Monday"
stdWeekDay // "Mon"
stdDay // "2"
stdUnderDay // "_2"
stdZeroDay // "02"
stdHour // "15"
stdHour12 // "3"
stdZeroHour12 // "03"
stdMinute // "4"
stdZeroMinute // "04"
stdSecond // "5"
stdZeroSecond // "05"
stdLongYear // "2006"
stdYear // "06"
stdPM // "PM"
stdpm // "pm"
stdTZ // "MST"
stdISO8601TZ // "Z0700" // prints Z for UTC
stdISO8601SecondsTZ // "Z070000"
stdISO8601ShortTZ // "Z07"
stdISO8601ColonTZ // "Z07:00" // prints Z for UTC
stdISO8601ColonSecondsTZ // "Z07:00:00"
stdNumTZ // "-0700" // always numeric
stdNumSecondsTz // "-070000"
stdNumShortTZ // "-07" // always numeric
stdNumColonTZ // "-07:00" // always numeric
stdNumColonSecondsTZ // "-07:00:00"
)