Date creation from a string

Asked

Viewed 21 times

0

I’m trying to create a Date object from a string of type yyyy-mm-dd.

Testing on a javascript console, for example:

new Date("2021-06-20")

The exit is:

Sat Jun 19 2021 21:00:00 GMT-0300 (Brasilia Standard Time)

Why the date returned is always the date of the previous day?

  • Making new Date("2021-6-20"), without zero before the month, the output is expected: Sun Jun 20 2021 00:00:00 GMT-0300 (Brasilia Standard Time)...

  • 1

    As already stated in reply in the link already indicated above, when placing the month without zero, the result varies from one browser to another (so I wouldn’t say it is a good solution). Anyway, there is explained why the problem occurs, and at the end of the answer has some options to circumvent it.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.