Thursday, March 23, 2023
HomeColdFusionJeffry Houser's Weblog: Extra on Date Conversions

Jeffry Houser’s Weblog: Extra on Date Conversions


I used to be lately engaged on an app the place the consumer was getting into one date, however the date was turning into one thing else. I hate it when this occurs, and even had comparable points within the early variations of my Study With e book sequence. Normally this can be a conversion subject between date entry within the UI, storage in backend service, and sending that knowledge again to the UI.

It drove me nuts, and I lastly discovered an answer. I made a decision to retailer all dates in UTC format, and move them on to the customers in the identical.

When the consumer enters a date within the UI, you possibly can flip it into UTC Format utilizing the JS toISOString() operate:



1let originalDate = new Date();
2let isoString = originalDate.toISOString()
3console.log(isoString);

It is best to see one thing like this in your console:


2022-11-09T14:41:56.202Z


Now ship this to your backend, and reserve it in your database as a date. All good!


When your REST Companies ship that date string again to the UI, you possibly can re-convert it right into a date object for show:



1let dateObject = new Date(isoString);
2console.log(dateObject);

The output ought to be one thing like this:



1Wed Nov 09 2022 09:41:56 GMT-0500 (Japanese Commonplace Time)

I discovered this to be a good way to deal with dates between the entrance finish and backend of an utility.

Play with a Plunker right here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments