0

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?

Thomas Fox
  • 521
  • 2
  • 5
  • 16
  • 3
    Your machine's timezone is most likely 1 hour ahead of UTC. – MC Emperor Jun 29 '23 at 07:30
  • Of course that explains it. I got too focused on the day, not the hour. Very inconvenient – Thomas Fox Jun 29 '23 at 07:33
  • 1
    _"How do I get the locale date object"_ There's no such thing. A `Date` represents a moment in time which is the same moment no matter where you are. – phuzi Jun 29 '23 at 07:57
  • If you want a timestamp like '06/06/2022' to be treated as UTC, then simply parse it yourself and treat it as UTC, e.g. assuming the timestamp is m/d/y: `let [m,d,y] = date.split(/\D/); let parsedAsUTC = new Date(Date.UTC(y, m-1, d))`. Using the built–in parser to parse it as local then try to convert to UTC is inefficient and fraught. Not to mention parsing an unsupported format with the built–in parser should be strongly discouraged, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jun 29 '23 at 10:12

3 Answers3

0

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:

process.env.TZ = 'Europe/Madri';
new Date('06/06/2022');
huan feng
  • 7,307
  • 2
  • 32
  • 56
  • This would be idea. However I am working within a TS environment and my DB ORM requires the full Date object. As I am only equating date values for writing tests, I am going to be equating the 2 dates in a manner similar to the above. – Thomas Fox Jun 29 '23 at 07:54
  • Does the 2nd solution match your needs? – huan feng Jun 29 '23 at 07:55
0

I have found a further solution using the answer provided by monster club at the following SO link..

creating date as UTC

Thomas Fox
  • 521
  • 2
  • 5
  • 16
  • 1
    This should be a comment, or included in the OP. It's not an answer. If that answer answers your question, then this is a duplicate and should be marked as such. – RobG Jun 29 '23 at 10:09
-1

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.

// '2023-06-05T23:00:00.000Z' this date is being generated within the test code
date1 = new Date('06/06/2022')

// '2023-06-05T00:00:00.000Z' this date is being generated by a DB ORM and returned from the unit test function
date2 = new Date(dResponse)

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.

Thomas Fox
  • 521
  • 2
  • 5
  • 16