Finding the nth day from today - JavaScript (JS Date)

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in a number n as the only input.

The function should first find the current day (using Date Object in JavaScript) and then the function should return the day n days from today.

For example −

If today is Monday and n = 2,

Then the output should be −

Wednesday

Example

Following is the code −

const num = 15;
const findNthDay = num => {
   const weekday=new Array(7);
   weekday[1]="Monday";
   weekday[2]="Tuesday";
   weekday[3]="Wednesday";
   weekday[4]="Thursday";
   weekday[5]="Friday";
   weekday[6]="Saturday";
   weekday[7]="Sunday"
   const day = new Date().getDay();
   const daysFromNow = num % 7;
   return weekday[(day + daysFromNow) % 7];
};
console.log(findNthDay(num));

Output

Following is the output in the console −

Friday
raja
Published on 18-Sep-2020 08:52:09
Advertisements