Get Upcoming Day Using Javascript

Here is a simple, generic Javascript (JS) code that will help you find what is the date for the next day of your choice.
Simple as 1-2-3:

/*
In JS, the week starts on Monday:
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
*/

// 1. Select the day of you choice
var dayInWeek = 4;

// 2. Get today's date
var today = new Date();

// 3. Calculate next day's date using this generic formula
var nextDayDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + dayInWeek - (today.getDay() >= dayInWeek ? today.getDay() - 7 : today.getDay()));
// Finally, do something with the found date, like displaying it to the visitor
alert(nextDayDate);

Don't forget to bookmark this - it may come in handy sometime.

Here is an example of use:

PS. In JS, the method getMonth() returns the number of the month in the year, zero based! This means that January returns as 0 and December returns as 11. If you get mixed up, don't say I didn't warn you...

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Image CAPTCHA