kat1330 kat1330 - 3 months ago 42
Javascript Question

Extract wrong Month from Date

I used following code:

var date = $('#datefromcal').val();
var dt = new Date(date);
alert(dt); // RESULT= Wed May 21 12:00:00 UTC+0200 2014
alert("Date : " + dt.getDate());//RESULT= Date : 21
alert("Month : " + dt.getMonth());//RESULT= Month : 4
alert("Year : " + dt.getFullYear()); //RESULT= Year : 2014

I try to extract Day, Month and Year. I allways received Month: 4, but now is May. My results are shown in comments.

Can somebody explain me why it happens? Or how to extract month.



The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).

The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.