0

Trying to get UTC day of the week for any given timestamp on any given machine (w/ their own local time) I used:

 var date = new Date(timestamp).toLocaleString('en-GB', { timeZone: 'UTC' });

Once I try to convert the date string to UTC date I get Invalid Date for some dates... it all seems pretty weird.

$ node
> date = new Date('15/08/2019, 00:00:00');
Invalid Date
> date = new Date('12/08/2019, 00:00:00');
2019-12-08T00:00:00.000Z
> date = new Date('15/08/2019'); 

Any idea where the Invalid Date issue may come from?

user3755529
  • 1,050
  • 1
  • 10
  • 30
  • '15/08/2019, 00:00:00' is not a format supported by ECMA-262, so parsing is implementation dependent. You might get a correct parse, incorrect parse, or invalid date. – RobG Aug 16 '19 at 00:47
  • 1
    If you're looking for a UTC day, just [`getUTCDay`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCDay). – AuxTaco Aug 16 '19 at 01:00

1 Answers1

0

By converting the timestamps to strings using the "en-GB" locale, it looks like you're getting them in DD/MM/YYYY format. But in your second example, the strings are being interpreted as "MM/DD/YYYY" in whatever your default locale is, so the first call fails because 15 isn't a valid month number.

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96