Datsik
Datsik

Reputation: 14824

Not able to parse time properly

I'm trying to parse time for values in a template like so:

"parseDate": func(timeStamp time.Time) string {
            newTime, err := time.Parse("Jan 2 2006 @ 15:04:05", fmt.Sprintf("%v", timeStamp))
            if err != nil {
                log.Println(err)
            }
            return fmt.Sprintf("%v", newTime)
},

which is one of my handler funcs, but I get this error:

parsing time "2015-12-13 06:49:52 +0000 UTC" as "Jan 2 2006 @ 15:04:05": cannot parse "2015-12-13 06:49:52 +0000 UTC" as "Jan"

Not sure what I'm doing wrong

Upvotes: 1

Views: 113

Answers (1)

0x434D53
0x434D53

Reputation: 1465

You have to parse it as

t, _ := time.Parse("2006-01-02 15:04:05 -0700 MST")

For parsing you have to give the format of the date you are receiving.

Then you can format the correctly parsed time using

t.Format("Jan 2 2006 @ 15:04:05")

Upvotes: 1

Related Questions