Go time.Parse() getting "month out of range" error
14,384
You're using the wrong reference time in the layout
parameter of time.Parse
which should be Jan 2, 2006 at 3:04pm (MST)
Change your begin
line to the following and it will work:
begin, err := time.Parse("2006-01-02 15:04:05", "2016-12-25 "+string(start)+":00")
Related videos on Youtube
Comments
-
rafaelmorais over 1 year
I'm new to Go and I was creating a little console script. You can check my code here:
package main import ( "bufio" "fmt" "os" "time" ) func main() { reader := bufio.NewReader(os.Stdin) fmt.Println("Calculate") fmt.Print("Hours and minutes: ") start, _, _ := reader.ReadLine() begin, err := time.Parse("2016-12-25 00:00:00", "2016-12-25 "+string(start)+":00") if err != nil { fmt.Println(err) } fmt.Println(begin) }
I've seen a related question but I couldn't understand why.
This is the error I'm getting after running my code:
parsing time "2016-12-25 22:40:00": month out of range 0001-01-01 00:00:00 +0000 UTC
Any ideas on what am I doing wrong?
Thanks
-
JimB over 7 yearsHow about the latest version of this question from a day ago? stackoverflow.com/questions/40388246/…
-
JimBHow else is the function supposed to know the difference in each field if there's no defined value?
-
-
rafaelmorais over 7 yearsDo you know why the layout needs to have this specific date?
-
nosequeldeebee over 7 yearsIt is inspired from Unix's epoch time en.wikipedia.org/wiki/Unix_time. It is Go's version. Go needs to have a set reference time for layout or else every programmer would just choose some arbitrary reference date.
-
musiphil over 6 yearsIt's not anything like the epoch, and that point in time doesn't mean anything like the Unix epoch does, but it's just a mnemonic for the elements of time (month, day, etc.). Think of it as
01/02 03:04:05PM '06 -0700
(1-2-3-4-5-6-7), and as long as you remember that single sequence, you can write1
,01
,Jan
, orJanuary
without having to remember%m
,%b
, or%B
as instrftime
. -
kio over 3 yearsthis should help: golang.org/src/time/format.go?s=25155:25201#L92