Why does new date () delete the day? - javascript

I am creating a date with new Date() . When I do this, he subtracts the day. Here is the code:

 var dateString = "2016-04-10"; var date = new Date(dateString); // date = Sat Apr 09 2016 18:00:00 GMT-0600 (MDT) 

What am I misunderstanding? Why is the date not Apr 10 2016 ? How can I do this job properly?

+3
source share
1 answer

Your time zone is GMT-6 (as shown in GMT-0600 (MDT) in your output). Therefore, the date that is generated is shifted by -6 hours. In this case, midnight minus 6 hours is 6 pm on the previous day.

If you call date.toISOString() , you will see that the UTC time is "2016-04-10T00:00:00.000Z" as expected.

+9
source

All Articles