-2

I want to convert a string time "2019-06-20 13:30:31" to int 20190620, I tried with below codes, but I got 10190101 instead, what's wrong with my trial?

func (u *Util) ConvertStringTimeToInt(stringTime string) (intTime int64) {
    timeLayout := "2019-01-01 01:01:01"
    timeOutput := "20190101"
    tmp, _ := time.Parse(timeLayout, stringTime)
    out := tmp.Format(timeOutput)
    outInt, _ := strconv.ParseInt(out, 10, 64)
    return outInt
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
jia Jimmy
  • 1,693
  • 2
  • 18
  • 38

1 Answers1

1

Time format uses specific values to denote time/date elements. 2006 is year, 01 is month, etc. So to parse that date, your layout must be:

timeLayout:="2006-01-02 15:04:05"

Once you parse it, there is an easier way of doing what you need:

outInt:=t.Year()*10000+int(t.Month())*100+t.Day()

Or, use similar layout to convert to string, and then to int.

Burak Serdar
  • 46,455
  • 3
  • 40
  • 59