Go: How to parse only date to time.Time?

Viewed 3897

I want to parse only date value to time.Time. For example I have date in this format: 2016-03-31, and I want to parse it, like: time.Parse(FORMAT, "2016-03-31").

But it always fail.

What is the correct format string to use to parse only date with this format?

I have the code below as example, it is on playground also: https://play.golang.org/p/0MNLr9emZd

package main

import (
    "fmt"
    "time"
)

var dateToParse = "2016-03-31"

func main() {
    format := "2006-12-01"
    parseDate(format)
}

func parseDate(format string) {
    t, err := time.Parse(format, dateToParse)
    if err != nil {
        fmt.Println("Format:", format)
        fmt.Println(err)
        fmt.Println("")
        return
    }
    fmt.Println("Works Format:", format)
    fmt.Println(t)
    fmt.Println("")
}

The output is this:

Format: 2006-12-01
parsing time "2016-03-31" as "2006-12-01": cannot parse "-31" as "2"
1 Answers
Related