I'm trying to parse this string pattern "4-JAN-12 9:30:14"
into a time.Time
.
Tried time.Parse("2-JAN-06 15:04:05", inputString)
and many others but cannot get it working.
I've read http://golang.org/pkg/time/#Parse and https://gobyexample.com/time-formatting-parsing but it seems there aren't any examples like this.
Thanks!
Edit: full code:
type CustomTime time.Time
func (t *CustomTime) UnmarshalJSON(b []byte) error {
auxTime, err := time.Parse("2-JAN-06 15:04:05", string(b))
*t = CustomTime(auxTime)
return err
}
parsing time ""10-JAN-12 11:20:41"" as "2-JAN-06 15:04:05": cannot parse ""24-JAN-15 10:27:44"" as "2"
Don't know what you did wrong (should post your code), but it is really just a simple function call:
Outputs:
Try it on the Go Playground.
Note that
time.Parse()
returns 2 values: the parsedtime.Time
value (if parsing succeeds) and an optionalerror
value (if parsing fails).See the following example where I intentionally specify a wrong input string:
Output:
Try it on the Go Playground.
EDIT:
Now that you posted code and error message, your problem is that your input string contains a leading and trailing quotation mark!
Remove the leading and trailing quotation mark and it will work. This is your case:
Output (try it on the Go Playground):