---
title: "Calculating day of the week for any Date in JavaScript"  
description: "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 bas"  
author: "Danish Khan"  
published: 2012-11-09  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/387/calculating-day-of-the-week-for-any-date-in-javascript  
category: "javascript"  
tags: ["javascript"]  
reading_time: 3 minutes  

---

# Calculating day of the week for any Date in JavaScript

##### Introduction:

In this blog I am going to explain how we can calculate the [day of the week](https://answers.mindstick.com/qa/46881/what-time-of-the-day-and-or-day-of-the-week-is-the-best-time-for-shopping-for-airfare) for any given date.

For calculating the day of the week is based on the following [Algorithm](https://www.mindstick.com/blog/119/implementing-cryptography-in-c-sharp-dot-net-by-using-sha1-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](https://www.mindstick.com/blog/12024/things-you-need-to-know-when-starting-your-own-business) from the index position 0 to 6.

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

The [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) 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](https://www.mindstick.com/forum/12711/select-query-with-and-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](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp) 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](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-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](https://www.mindstick.com/articles/12845/things-to-remember-while-migrating-odoo-to-a-better-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](https://www.mindstick.com/forum/23089/in-java-how-can-i-test-if-an-array-contains-a-certain-value) the names of days. The [expression](https://www.mindstick.com/articles/1861/sqlite-expressions) 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.

---

Original Source: https://www.mindstick.com/blog/387/calculating-day-of-the-week-for-any-date-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
