'Incorrect date shown in new Date() in JavaScript

enter image description here

This is what I get in chrome console. I pass "2016-09-05"(YYYY-MM-DD) as the date and it shows me Sept 4,2016 as the date.

Another constructor shows the right date

enter image description here

Passing it comma separated needs some tokenizing + parsing + making month zero indexed which I want to avoid



Solution 1:[1]

You could use the Solution from UTC Date Conversion. Which basicall does the following:

console.log(new Date("2014-12-23"));
console.log(convertDateToUTC(new Date("2014-12-23")));

function convertDateToUTC(date) { 
return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()); 
}

The output would be like that in the console (for me at least :D)

Tue Dec 23 2014 01:00:00 GMT+0100 (Mitteleuropäische Zeit)
Tue Dec 23 2014 00:00:00 GMT+0100 (Mitteleuropäische Zeit)

Solution 2:[2]

use setFullYear the syntax is Date.setFullYear(year,month,day)

mydate = new Date();
mydate.setFullYear(2016, 08, 05);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 konenas