Home > Back-end >  Parsing time from unvalid string "24-Sep.-20" in Go
Parsing time from unvalid string "24-Sep.-20" in Go

Time:10-04

I've try to parse this string "24-Sep.-20" use any method, but still not resolved because that string has a period.

I want convert my date to "24-09-2020".

How can I do this?

CodePudding user response:

Simply just by update your time layout by adding (.) before the (-), and then reformat your parsed time to the new layout.

func main() {
    date := "24-Sep.-20"
    layout := "02-Jan.-06"
    t, _ := time.Parse(layout, date)

    newLayout := "02-01-2006"
    output := t.Format(newLayout)
    fmt.Println(output)
}

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

  •  Tags:  
  • go
  • Related