Home > front end >  How to handle the same DateTime value on frontend and in backend?
How to handle the same DateTime value on frontend and in backend?

Time:11-30

I created an Angular app that has input field for DateTime value. This value is converted into local time with Moment.js and saved into a database. I also have a hosted VM with a console app that checks this DateTime and if its equal or lower than current 'DateTime.Now' and then sends an email.

The problem is, I don't want to use 'DateTime.Now' because the date can be entered by anyone in any country and the VM has a different timezone and/or date set on computer so in my opinion the process would be like this:

User A is from New York so when entering the date (example: Monday 5th Dec. 2021, 8:00 AM, the Date is saved as that value. And the VM is hosted somewhere in Europe where the current time is 11 PM on same day, but the console app should read the date value from the user who lives in New York and should check the current time in New York.

How can I accomplish this? Should I send the name of Timezone in frontend app and save that information in a separate field or should I save the date in a different format? And what library can I use on backend console app that would check the current time for country where user lives?

CodePudding user response:

You can use ISO date format. It works well with timezone. Just use new Date(isoTime), where isoTime is value in ISO format, like this: 2021-11-29T15:43:27.626Z. When you do that, client's browser checking timezone for you and displays correct time for the user.

And in other side, you can convert any local time to UTC, just use new Date().toISOString(). You should place your date value instead new Date() here.

You don't need moment for this.

  • Related