Home > Blockchain >  Is new Date(...) object and constructor good for international product?
Is new Date(...) object and constructor good for international product?

Time:12-18

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

CodePudding user response:

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:

const local_date = new Date(iso_date);
  • Related