When I define time like this in js
{expiry:new Date()}
and create a struct in go endpoints like this
{Expiry time.Time `json:"expiry"`}
I get a parse error from go
"parsing time \"\"2006-01-02T15:04:05Z07:00\"\" as \"\"2006-01-02T15:04:05Z07:00\"\": cannot parse \"07:00\"\" as \"\"\""
Any suggestions?
When I define time like this in js
{expiry:new Date()}
and create a struct in go endpoints like this
{Expiry time.Time `json:"expiry"`}
I get a parse error from go
"parsing time \"\"2006-01-02T15:04:05Z07:00\"\" as \"\"2006-01-02T15:04:05Z07:00\"\": cannot parse \"07:00\"\" as \"\"\""
Any suggestions?
Share Improve this question edited Jan 8, 2014 at 14:49 Gert Cuykens asked Jan 8, 2014 at 1:49 Gert CuykensGert Cuykens 7,17514 gold badges53 silver badges87 bronze badges 2- 1 I think this answer will help you: stackoverflow./a/20476078/683201 – Eve Freeman Commented Jan 8, 2014 at 2:22
- ok, that would be a go solution, what about a js solution to reformat time string so endpoints accept? – Gert Cuykens Commented Jan 8, 2014 at 3:16
1 Answer
Reset to default 8The documentation for time.UnmarshalJSON states:
UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.
There is a problem that all browsers doesn't necessarily encode DateTime
objects into RFC3339 format. However, your error message doesn't seem to imply that. You seem to try to encode the following JSON string:
"2006-01-02T15:04:05Z07:00"
That is not a timestamp, but rather the time
package's reference layout. See this Playground example that shows how Go expects a timestamp to be like: http://play.golang/p/4NQ1pRidPt
However, there is still that problem with browser inconsistency. To avoid this you can use a function or library, as @elithrar suggested:
var a = {expiry: moment(new Date()).format("YYYY-MM-DDTHH:mm:ssZ")};
console.log(a);
Output:
{"expiry": "2014-01-08T08:54:44+01:00"}
JSFiddle