dateparsinggo

How to parse a date string in a foreign language with go time


So my problem is: I want to do this:

datestring := "19. april 2018"

parsedDate, err := time.Parse("2. January 2006", datestring)
if err != nil {
    fmt.Println(err)
}

fmt.Println(parsedDate)

This snippet works perfectly... but now my input datestring isn't english... it's german. So april fooled me. (German April = English April). Running this with datestring := "19. Februar 2018" fails:

parsing time "12. februar 2018" as "2. January 2006": 
    cannot parse "februar 2018" as "Januar\"

Is there any way to add parseable (natural) languages? Or define the language I expect. I didn't find any mention of this in the documentation.

Thanks!


Solution

  • I believe what you are trying to achieve can be accomplished using the package

    https://godoc.org/github.com/goodsign/monday

    It allows you to specify that you want to parse the date in the German locale using the ParseInLocation function.

    A good example on how to use this (and in german) can be seen in this question.