When converting the date string ’06/06/2022′ to a date object in my local node environment using the following code…
new Date('06/06/2022')
The resulting date object is ‘2023-06-05T23:00:00.000Z’.
The day is being changed.
I understand that I can use .toLocaleDateString() to convert this back. But I require the date object itself.
How do I get the locale date object, instead of the locale date string?
3
Answers
As MC emperor mentioned, the local machine date is out from UTC by 1 hour.
I was attempting to equate 2 date values whilst writing server tests and running the server locally.
To handle this in the short term at least I have used the following solution.
To equate the 2 dates. I have normalized the UTC values using .setUTCHours(0) Another solution may have been to simply add a day to the ORM response for the purposes of equating the test.
However both of these are simply hacks/workarounds. I am unsure as to a perfect solution here.
Maybe you should not use the date object, as it represents a timezone-agnostic timestamp, and uniquely defines an instant in history. the problem you are facing is the display of node.
If you only focus on the day, you have options like:
Passing date as a text value: ’06/06/2022′
this will not be accurate if you use the value in different timezones
If you only want the node console to be correct, you can change timezone of node environment to something like:
I have found a further solution using the answer provided by monster club at the following SO link..
creating date as UTC