go - How to parse only date to time.Time? -
i want parse date value time.time
. example have date in format: 2016-03-31
, , want parse it, like: time.parse(format, "2016-03-31")
.
but fail.
what correct format string use parse date format?
i have code below example, 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 this:
format: 2006-12-01 parsing time "2016-03-31" "2006-12-01": cannot parse "-31" "2"
these predefined layouts use in time.format , time.parse. reference time used in layouts specific time:
mon jan 2 15:04:05 mst 2006
which unix time 1136239445. since mst gmt-0700, reference time can thought of as
01/02 03:04:05pm '06 -0700
to define own format, write down reference time formatted way; see values of constants ansic, stampmicro or kitchen examples.
use format := "2006-01-02"
yyyy-mm-dd.
Comments
Post a Comment