-5

i need to convert a date like this new Date(2018, 12, 24, 10, 33) but it returns like this Thu Jan 24 2019 10:33:00 GMT+0530 (India Standard Time) any idea. For other months its working fine. Even i checked in w3school

There also it return wrong date please give me any solution

Kresimir
  • 777
  • 5
  • 20
Bipin
  • 334
  • 6
  • 21
  • 1
    Please post your complete code that is returning this string. What are you doing in between? – YetiCGN Dec 07 '18 at 09:09
  • 4
    Possible duplicate of [Creating date with numbers (new Date(2012, 03, ...) gives wrong month (+1)](https://stackoverflow.com/questions/10649036/creating-date-with-numbers-new-date2012-03-gives-wrong-month-1) – Phil Ross Dec 07 '18 at 09:11

2 Answers2

1

Because in javascript Date object months are numbered from 0 (January) to 11 (December). 12 in your new Date() code is not a valid month, so javascript converts it to 0, returning you a date in January.

azawaza
  • 3,065
  • 1
  • 17
  • 20
Luca Antonelli
  • 349
  • 1
  • 2
  • 21
1

There is no issue in javascript function.

In Javascript months start with 0 to 11, if you pass 0 will return January and 11 will return december.

Somehow JavaScript returning January during insertion of 12 month in digit instead of error but you achieved your result by 0 to 11 month.

Here is code for your reference.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript new Date()</h2>

<p>5 numbers specify year, month, day, hour, and minute:</p>

<p id="demo"></p>
<p id="demo1"></p>
<script>
var d = new Date(2018, 0, 24, 10, 33);
document.getElementById("demo").innerHTML = d;

var d1 = new  Date(2018, 11, 24, 10, 33)
document.getElementById("demo1").innerHTML = d1;

</script>
</body>
</html>
AGH
  • 353
  • 1
  • 14