How to use time.Parse to parse hh:mm format in Go

Viewed 7154

I am importing a lot of fields of the format:

09:02 AM
10:02 AM
12:30 PM
04:10 PM
04:50 PM
05:30 PM

I would like to convert the fields into something I can do arithmetic on. For example, do a count down to when the event Occurs. Thus, saving the field in microseconds... or even seconds. I have been trying to get the time.Parse to work... no joy.

fmt.Println(time.Parse("hh:mm", m.Feed.Entry[i].GsxA100Time.T))

returns...

0001-01-01 00:00:00 +0000 UTC parsing time "07:50 PM" as "hh:mm": cannot parse "07:50 PM" as "hh:mm"

any suggestions?

4 Answers

Just in case anyone is working with epoch times can use this,

func convert (in int64) string {
    u := time.Unix(in/1000, in%1000)
    ampm := "AM"
    if u.Hour()/12 ==1 { ampm = "PM" }
    th := u.Hour()%12
    hh := strconv.Itoa(th)
    if th < 10 { hh = "0" + hh }
    tm := u.Minute()
    mm := strconv.Itoa(tm)
    if tm < 10 { mm = "0" + mm }
    return hh + ":" + mm + " " + ampm
}
Related