Date.getDay() javascript returns wrong day
Hi I'm new in javascript I have such javascript code
alert(DATE.value);
var d = new Date(DATE.value);
var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDay();
alert(month);
alert(day);
if(2012 < year < 1971 | 1 > month+1 > 12 | 0 >day > 31){
alert(errorDate);
DATE.focus();
return false;
}
take for instance: DATE.value = "11/11/1991"
when I call alert(day);
it shows me 3
;
when I call alert(d);
it is returns me correct info.
Solution 1:
use .getDate
instead of .getDay
.
The value returned by getDay is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
Solution 2:
getDay()
returns the day of the week. You can however use the getDate()
method.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getDay
Solution 3:
getDay()
will give you the day of the week. You are looking for getDate()
.
Solution 4:
I had a similar problem. date.getMonth()
returns an index ranging from 0 to 11
. January is 0
. If you create a new date()
-object and you want to get information about a costum date not the current one you have to decrease only the month by 1
.
Like this:
function getDayName () {
var year = 2016;
var month = 4;
var day = 11;
var date = new Date(year, month-1, day);
var weekday = new Array("sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday");
return weekday[date.getDay()];
}