Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is new Date(…) object and constructor good for international product?

I have a calendar in an app that i am building for a client, i want to know if new Date() in javascript is enough to get all dates and times correct based on user location and time when deployed to production… or will it cause any problem and give wrong timezones/dates? Some people told me to use libraries like momentjs instead, any specific reason why?

I tried to do some tests and already i am not getting my timezone when passing a custom date, i am getting UTC dates

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In order to have everything consistent, the good practice is to convert everything on client side to the ISO 8601 format before sending it to the sever.

ISO format is independent of the time zone, and you can easily convert any date with built-in .toISOString() method:

const iso_date = new Date().toISOString();

Later, when you send back the data to the client from the server, you can parse it back to the user’s local time zone with .parse() method:

const local_date = Date.parse(iso_date);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading