catamphetamine / react-time-ago

Localized relative date/time formatting in React

Home Page:https://catamphetamine.github.io/react-time-ago/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Time is 6 hours behind current time

mdodge-ecgrow opened this issue · comments

I'm pulling my times from a database. I've looked at the DB and the times are correct there. I pull them in and display them in my app using this library and they are always 6 hours behind.
I am setting the locale in my index.js like so:

...
import JavascriptTimeAgo from 'javascript-time-ago';
import en from 'javascript-time-ago/locale/en';
...
JavascriptTimeAgo.locale(en);
...

FYI, I am in CST timezone (UTC -6 | CST | Chicago).
More info: the date in the DB is an ISO 8601 format (2020-02-20T15:04:02.503Z). And once I get it into my javascript I am calling Date.parse on it so it will work correctly in react-time-ago.

Time is 6 hours behind current time

FYI, I am in CST timezone (UTC -6 | CST | Chicago).

Isn't it obvious that you store dates in a database in UTC0, so "time is 6 hours behind current time".

More info: the date in the DB is an ISO 8601 format (2020-02-20T15:04:02.503Z). And once I get it into my javascript I am calling Date.parse on it so it will work correctly in react-time-ago.

react-time-ago doesn't accept strings.
Of course it is to be parsed.

Sorry, this is not obvious to me, that is why I asked about it. So I need to just add 6 hours to it after I grab it from the DB or is there a more robust fix?

Depends on the database.
For example, PostgreSQL and MySQL don't store time zone in the database, so dates are always UTC0 there.
It's the database software UI that adjusts the time.
In your case, you didn't provide the code, but seems like you're passing a String instead of a Date which is not supported.
So first parse your dates as you already do, then pass them to the component.

And once I get it into my javascript I am calling Date.parse on it so it will work correctly in react-time-ago.

So it works then, as you say.