Get date tomorrow using getDay Javascript

What I'm doing is a weather forecast website, and I need the days of the week (like Sunday, Monday, etc.). To get the date tomorrow, I just put "+ 1", as someone suggested in another question, but when it gets to Saturday, it says "undefined". How can I do this when he gets to Saturday + 1 will switch on Sunday? Thanks in advance!

var day=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

document.getElementById('tomorrow').innerHTML = weekday[day.getDay() + 1];
document.getElementById('twodays').innerHTML = weekday[day.getDay() + 2];
document.getElementById('threedays').innerHTML = weekday[day.getDay() + 3];
+5
source share
2 answers

Use (day.getDay() + i) % 7. This will return results between 0-6.

+7
source

To add a day to the javascript Date object you would do:

var date =new Date();
//use the constructor to create by milliseconds
var tomorrow = new Date(date.getTime() + 24 * 60 * 60 * 1000);

Notice, get the date .

Date.getDay 0 6 , .

, :

var date =new Date();
var tomorrow = new Date(date.getTime() + 24 * 60 * 60 * 1000);
var twoDays = new Date(date.getTime() + 2 * 24 * 60 * 60 * 1000);
var threeDays  = new Date(date.getTime() + 3 * 24 * 60 * 60 * 1000);

document.getElementById('tomorrow').innerHTML = weekday[tomorrow.getDay()];
document.getElementById('twodays').innerHTML = weekday[twoDays.getDay()];
document.getElementById('threedays').innerHTML = weekday[threeDays.getDay()];

:

+26

All Articles