blog

Home / DeveloperSection / Blogs / Calculating day of the week for any Date in JavaScript

Calculating day of the week for any Date in JavaScript

Danish Khan13115 09-Nov-2012
Introduction:

In this blog I am going to explain how we can calculate the day of the week for any given date.

For calculating the day of the week is based on the following Algorithm

Algorithm

a = (14 - month) / 12

y = year - a

m = month + 12 * a - 2

d = (day + y + y / 4 - y / 100 + y / 400 + (31 * m / 12) % 7

Array  daysofweek contains all the days name starting from the index position 0 to 6.

Array monthsofayear contains all the months name indexes starting from 0 to 11.

The function AddNths is used to add the suffix after the day of the month such as 22nd , 1st etc. It checks if the day is 1 or 21 or 31 then it will return day with the suffix st. If the day is 2 or 22 then it will return the suffix  nd, same concept is applied for the 3 and 23 and if condition is true then the suffix rd will be returned,  if no matches are found then th will return

The function DispFullDate(day, month, year) takes three parameters first one for day, second one for month and the third one for year. After calculating the day it will return the concatenated string of day of the week followed by day of the month then month of a year and after all the year.

Example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>To Calculate day based on the date </title>
    <script type="text/javascript">
        function CalDayOfWeek(day, month, year) {
            var a = Math.floor((14 - month) / 12);
           var y = year - a;
            var m = month + 12 * a - 2;
            var d = (day + y + Math.floor(y / 4) - Math.floor(y / 100) +
            Math.floor(year / 400) + Math.floor((31 * m) / 12)) % 7;
            return d;
        }
        var daysofaweek = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday',       
         'Thursday', 'Friday', 'Saturday');
        var monthsofayear = new Array('January', 'February', 'March', 'April', 'May',
          'June','July', 'August', 'September', 'October', 'November', 'December');
        function AddNths(day) {
            if (day == 1 || day == 21 || day == 31) return 'st';
            if (day == 2 || day == 22) return 'nd';
            if (day == 3 || day == 23) return 'rd';
            else {
                return 'th';
            }
        }
        function DispFullDate(day, month, year) {
            return daysofaweek[CalDayOfWeek(day, month, year)] + ' ' +
              day + AddNths(day) + '  ' + monthsofayear[month - 1] + '  ' + year;
        }
        document.write(DispFullDate(22, 10, 2012));
    </script>
  </head>
</html>

Output:
Monday 22nd October 2012

JavaScript has provided us getDay() function to get the day of the week we have to enter the date according to the date the user has typed in the text box it will display the day of the week. It is a compact version of the above code and is very useful also.

Example:
    <script type="text/javascript">
        function getTheDay(aText) {
            myDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",   
            "Saturday", "Sunday"]
            myDate = new Date(eval('"' + aText.value + '"'))
            document.form1.textDay.value = myDays[myDate.getDay()]
       }
    </script>

In this example myDays array contains the names of days. The expression which the user typed is evaluated with eval() keyword and the current day of the week is accessed through the function myDays[myDate.getDay()].

Conclusion:

Through this blog we came to know about how we can get the day of the week for any given Date in JavaScript.

 

 


Updated 18-Sep-2014

Leave Comment

Comments

Liked By