Home > database >  How to parse time with unique format in golang?
How to parse time with unique format in golang?

Time:09-23

I have date time with format 18/09/21 14.56 from excel. I want parse to format 2006-01-02 hh:mm:ss. But I got error while parsing the time.

Is possible parsing time with format 18/09/21 14.56 in golang?

CodePudding user response:

func TestTime(t *testing.T) {
    tm, err := time.Parse("06/01/02 15.04", "18/09/21 14.56")
    if err != nil {
        return
    }
    log.Println(tm.Format("2006-01-02 15:04:05"))
}

CodePudding user response:

Golang use example based template for Parse and Format.

01   -> month with zero prefix
02   -> day with zero prefix
06   -> year (last two digits)
15   -> hour (24h based)
04   -> minutes with zero prefix
05   -> seconds with zero prefix
2006 -> long year
t, _ := time.Parse("02/01/06 15.04", "18/09/21 14.56")

t.Format("2006-01-02 15:04:05") // 2021-09-18 14:56:00

For more constants see https://stackoverflow.com/a/20234207/12301864

  •  Tags:  
  • go
  • Related