Skip to content Skip to sidebar Skip to footer

Get Tomorrow's Date With GetDay Javascript

What I am making is a weather forecast website, and what I need is the days of the week (ie. 'Sunday', 'Monday', etc.). To get tomorrow's date I am just putting '+ 1' like someone

Solution 1:

To add a day to a 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);

Note, getDate.

Date.getDay returns a number from 0-6 based on what day of the week it is.

So you would do:

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()];

Edit: Fixing typo


Solution 2:

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


Post a Comment for "Get Tomorrow's Date With GetDay Javascript"