-2

I have date like this: December, 08 2020

and I need to convert it in format similar to this: 2020-02-03T09:00:00

(time part must be there but it can be whatever, time is not relevant)

  • 1
    Welcome, this is not how StackOverflow works. Please read https://stackoverflow.com/help/how-to-ask – jasie Jul 01 '20 at 09:23
  • Dates typically don't have an associated timezone, they're usually treated as local so 8 Dec 2020 is the same date everywhere. Adding a timezone means it may be 7 Dec 2020 in about half the world. – RobG Jul 01 '20 at 09:51

1 Answers1

-1

Welcome to SO.

you can simply use

console.log(new Date('December, 08 2020 00:00:00 GMT').toISOString())

Appending the string ' 00:00:00 GMT' with help you in getting consistent result across timezones.

simply-put
  • 1,068
  • 1
  • 11
  • 20
  • also gives `2020-12-07T23:00:00.000Z` for me in central europe – Argee Jul 01 '20 at 09:27
  • @Argee We can simply append 00:00:00 GMT string to make sure it doesn't change according to timezone – simply-put Jul 01 '20 at 09:29
  • 1
    See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jul 01 '20 at 09:49
  • interpreting the string with an own implementation seems to be the way to go then? – Argee Jul 01 '20 at 10:41
  • @Argee, Sorry you're saying the day change but output is 2020-12-08T00:00:00.000Z. Where is the day change in my proposed solution after edit? – simply-put Jul 01 '20 at 13:48
  • there is not any... in the pre-edit version there was, like you said yourself :D – Argee Jul 01 '20 at 13:59