time.Parse错误,我在做什么错?

I'm running into the most bizarre issue regarding datestring parsing in golang using the time package.

The Error:

parsing time "07-20-2018" as "2006-01-02": cannot parse "0-2018" as "2006"

The Code Block:

log.Println(datestring) //07-20-2018
date, err := time.Parse("2006-01-02", datestring)
log.Println(err) //parsing time "07-20-2018" as "2006-01-02": cannot parse "0-2018" as "2006"
log.Println(date) //parsing time "07-20-2018" as "2006-01-02": cannot parse "0-2018" as "2006"

I'm completely at a loss to what this issue is referring to, the string is parsed from the URI in golang with gorilla mux.

datestring, _ := vars["date"] //some/path/{date}, date is 07-20-2018

Any ideas?

It is obvious. You are trying to parse mm-dd-yyyy as yyyy-mm-dd.


A simple fix:

package main

import (
    "fmt"
    "time"
)

func main() {
    datestring := "07-20-2018"
    fmt.Println(datestring)
    date, err := time.Parse("01-02-2006", datestring)
    fmt.Println(date, err)
}

Playground: https://play.golang.org/p/gK7cMAkrP7l

Output:

07-20-2018
2018-07-20 00:00:00 +0000 UTC <nil>

See Go Package time.