I have an app that creates some entities (it doesn't really matter for this question), but the entities could be created in different timezones, so I have to store UTC offset in my createdAt and updatedAt columns. For some reasons, I'm not able to save generated date correctly, please refer to the code snippet below, to get more details:
Here is what I'm doing:
const { user } = userInfoWebAPICallResult as WebAPICallResult
const now = moment(moment.now())
.tz(user.tz!)
.toDate();
const entity = await Entity.create({
...someData,
createdAt: now
updatedAt: now,
});
I'm getting tz value externally, it's a string which looks like "America/Los_Angeles". If I convert now to string (by calling .format() method) and log the received value I will see something like this "2020-01-03T16:32:53-08:00". As you can see the date has offset value, this is the correct date, but during inspecting the Database I see "2020-01-04 00:32:53.013 00", this string doesn't have offset value anymore and it looks like the resulting date was computed automatically.
I will need to implement the filtration by createdAt date, people from different timezone will use this filtering mechanism. I planned to expose two parameters date and utcOffset via API, which would allow me to return correct entries depending on the user time zone.
What I'm missing?
tzin the database? What if I store dates as a number, by converting them in the following way: ` const now = moment .utc(moment.now()) .utcOffset(user.tz_offset) .unix(); ` That should give a number which already aligned with timezone Regarding DB inspection. I just selected all rows from the table using TablePlusnowvariable is exactly the same as justconst now = new Date();