How can I make go Time compatible with js Date
go, google-app-engine, google-cloud-endpoints, javascript
Solution
The 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.org/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
Problem
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?