
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding day of week from date (day, month, year) in JavaScript
We are required to write a JavaScript function that takes in three argument, namely:day, month and year. Based on these three inputs, our function should find the day of the week on that date.
For example: If the inputs are −
day = 15, month = 8, year = 1993
Output
Then the output should be −
const output = 'Sunday'
Example
The code for this will be −
const dayOfTheWeek = (day, month, year) => { // JS months start at 0 return dayOfTheWeekJS(day, month - 1, year); } function dayOfTheWeekJS(day, month, year) { const DAYS = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', ]; const DAY_1970_01_01 = 4; let days = day − 1; while (month − 1 >= 0) { days += daysInMonthJS(month − 1, year); month −= 1; } while (year − 1 >= 1970) { days += daysInYear(year − 1); year −= 1; } return DAYS[(days + DAY_1970_01_01) % DAYS.length]; }; function daysInMonthJS(month, year) { const days = [ 31, // January 28 + (isLeapYear(year) ? 1 : 0), // Feb, 31, // March 30, // April 31, // May 30, // June 31, // July 31, // August 30, // September 31, // October 30, // November 31, // December ]; return days[month]; } function daysInYear(year) { return 365 + (isLeapYear(year) ? 1 : 0); } function isLeapYear(year) { return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0; } console.log(dayOfTheWeek(15, 8, 1993));
Explanation
We want to count how many days until the given date. To do so, we can start at the infamous Unix time 0 (Thursday 1970−01−01) and go from there −
Count days in complete years
Count days in month for the incomplete year
Remaining days for the incomplete month
Output
And the output in the console will be −
Sunday
- Related Articles
- How to get day of month, day of year and day of week in android using offset date time API class?
- Create date from day, month, year fields in MySQL?
- How to get a Date from year, month and day in Java?
- Convert day of year to day of month in Java
- Format MySQL date and convert to year-month-day
- Finding the nth day from today - JavaScript (JS Date)
- Determine day of week in month from Gregorian Calendar in Java
- Fetch month, day, year, etc. from ISODate in MongoDB?
- Group by day/month/week based on the date range in MongoDB
- Combine three strings (day, month, year) and calculate next date PHP?
- How to Convert Numbers to Year/Month/Day or Date in Excel?
- How to convert year, month, and day of the month into a complete date in R?
- How to display first day and last day of the month from date records in MySQL?
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- Java Program to get day of week for the last day of each month in 2019

Advertisements