Date.getDay() javascript returns wrong day

JavascriptDate

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

getDay() will give you the day of the week. You are looking for getDate().

Solution 4 - Javascript

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()];
}

Solution 5 - Javascript

From now on you probably want to use the following below functions for Date objects:

    function dayOf(date)
    {
        return date.getDate();
    }

    function monthOf(date)
    {
        return date.getMonth() + 1;
    }

    function yearOf(date)
    {
        return date.getYear() + 1900;
    }

    function weekDayOf(date)
    {
        return date.getDay() + 1;
    }
    
    var date = new Date("5/15/2020");
    console.log("Day: " + dayOf(date));
    console.log("Month: " + monthOf(date));
    console.log("Year: " + yearOf(date));

Solution 6 - Javascript

function formatDate(date, callback)
{
var weekday = new Array("Sunday", "Monday", "Tuesday", "Wednesday",     "Thursday", "Friday", "Saturday");
var day = weekday[date.getDay()];
console.log('day',day);
var d = date.getDate();
var hours = date.getHours();
ampmSwitch = (hours > 12) ? "PM" : "AM";
if (hours > 12) {
    hours -= 12;

}
else if (hours === 0) {
    hours = 12;
}
var m = date.getMinutes();
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var month = months[date.getMonth()];
var year = date.getFullYear();
newdate = day + ', ' + month + ' ' + d + ',' + year + ' at ' + hours + ":" + m + " " + ampmSwitch
callback(newdate)
}

and call with this code

date="Fri Aug 26 2016 18:06:01 GMT+0530 (India Standard Time)"
formatDate(date,function(result){
   console.log('Date=',result);
 });

Solution 7 - Javascript

When you use the function .getDay(), this one consider the month's number start in 0 and not in 1, then if you use the date new Date(2021-11-04), the .getDay() function will consider the day 4 of dezember and not november...

Then, to get de day 4 of november, you need pass "2021-10-04" to the Date() class.

Solution 8 - Javascript

It may also be an issue with the technology used and the browser ES5 compatible.

I had such a problem while developing an application in react native.

return `${date.getDay()} ${month}, ${date.getFullYear()}`;

to

return `${date.getDate()} ${month}, ${date.getFullYear()}`;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAleksei BulgakView Question on Stackoverflow
Solution 1 - JavascriptLuca RainoneView Answer on Stackoverflow
Solution 2 - JavascriptOlicalView Answer on Stackoverflow
Solution 3 - JavascriptAsh BurlaczenkoView Answer on Stackoverflow
Solution 4 - JavascriptSven YaView Answer on Stackoverflow
Solution 5 - Javascriptuser13710070View Answer on Stackoverflow
Solution 6 - JavascriptAlexView Answer on Stackoverflow
Solution 7 - JavascriptFilipe MacielView Answer on Stackoverflow
Solution 8 - JavascriptGoliahtzView Answer on Stackoverflow