JavaScript > Date

Get day name of date in JavaScript

How to return the name of the current day in JavaScript?


getDay()

getDay() is an date method in JavaScript, which is used to return the day of the week (Name/number of the day in a week between 0 to 6) for the specified date.

As we all know there are 7 days in a week, the numbering for those days in JavaScript goes as follow,

Sunday=0, Monday=1, Tuesday=2,................Saturday=6.

This method does not have any parameters.

In JavaScript, by using getDay() we can return the name of the present day.

Returning the name of the present day in a week.

<p>Click the below button to get the name of the Day.</p>
<input type="button" value="Click" onclick="myDate()" />
<p id="myId"></p>

<script>
    function myDate() {
        var a = new Date();
        var weekdays = new Array(7);
        weekdays[0] = "Sunday";
        weekdays[1] = "Monday";
        weekdays[2] = "Tuesday";
        weekdays[3] = "Wednesday";
        weekdays[4] = "Thursday";
        weekdays[5] = "Friday";
        weekdays[6] = "Saturday";
        var r = weekdays[A.getDay()];
        document.getElementById("myId").innerHTML = r;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function myDate() in the script code which is connected to the Onclick of the HTML button.

We have given newDate() to the variable a, which returns the day, date, year, month & time. There are 7 days in a week, we are representing all these 7 days as an Array with the variable "weekdays".

Onclick of the button fires the function myDate() in the script code at the same time getDay() return the name of the present day as output.

OUTPUT

 

Returning day (between 0 to 6) of the week.

<p>Click the below button to day of a week.</p>
<input type="button" value="Get Day" onclick="myDate()" />
<p id="myId"></p>

<script>
    function myDate() {
        var a = new Date();
        var r = a.getDay();
        document.getElementById("myId").innerHTML = r;
    }
</script>

Returning the day of the week

We are returning the day (Number) of a week by using getDay() method in JavaScript.

OUTPUT

 Views: 97755 | Post Order: 100



Write for us






Hosting Recommendations