Reputation: 15730
I have a legacy database where dates are stored without timezone information. I need to read these into the client at face value, that is, 2 pm
in the server timezone should be interpreted as 2 pm
in the client timezone.
I was able to remove timezones when querying the server by replacing Date.prototype.toJSON
with a method that simply ignores timezones. Now I need a similar solution server-side.
This is not a duplicate of how to set local timezone in sails. I want to unset it.
EDIT
The dates are correct in PostGRES
, but Waterline/Sails seem to translate it to UTC before turning it into JSON. I don't want this to happen. A query in PostGRES returns 2013-02-28 15:00:00
, but when I console.log the result from the same query in Sails (through a custom made toJSON method) I get "2013-02-28T07:00:00.000Z"
instead. And then it gets sent to the client and translated to the client's local time... you get the picture.
Upvotes: 4
Views: 1588
Reputation: 15730
So sails-postgresql timezone support is broken. This is my workaround. I parse the date string back to a JavaScript Date instance, set it to UTC time, then return the ISO string with the timezone chopped off. The client never complains. :)
This is all done in api/model/theModel.js
, as explained in the docs.
toJSON: function() {
var obj = this.toObject();
if (obj.datetime) {
var date = new Date(Date.parse(obj.datetime)),
y = date.getFullYear(),
m = date.getMonth(),
d = date.getDate(),
h = date.getHours(),
M = date.getMinutes(),
newDate = new Date();
newDate.setUTCFullYear(y);
newDate.setUTCMonth(m);
newDate.setUTCDate(d);
newDate.setUTCHours(h);
newDate.setUTCMinutes(M);
obj.datetime = newDate.toISOString().slice(0,-8);
}
return obj;
},
Upvotes: 1