Get tomorrow's date with getDay Javascript
Asked Answered
F

3

6

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 suggested in another question, but when it get's to Saturday, it says "undefined". How do I make it so when it gets to Saturday, + 1 will loop over to 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];
Fosterfosterage answered 14/2, 2012 at 4:22 Comment(0)
N
9

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

Neocene answered 14/2, 2012 at 4:25 Comment(3)
Thanks so much fayerth! Don't know what andyortlieb is talking about :PFosterfosterage
Sorry I was trying to be clever. The code is correct, but luckily only the explanation is off-by-one ;) 0-6.Milled
@Milled Thanks for the catch. I saw that and fixed it. :)Neocene
M
28

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

Magness answered 14/2, 2012 at 4:27 Comment(1)
Thanks but that's not really was looking for. Fayerth answered my question though, thanks.Fosterfosterage
N
9

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

Neocene answered 14/2, 2012 at 4:25 Comment(3)
Thanks so much fayerth! Don't know what andyortlieb is talking about :PFosterfosterage
Sorry I was trying to be clever. The code is correct, but luckily only the explanation is off-by-one ;) 0-6.Milled
@Milled Thanks for the catch. I saw that and fixed it. :)Neocene
L
0

Just adding clarity on seeming's answer. i in this case is the integer so for example the next two days would look like:

  document.getElementById('twodays').innerHTML = weekday[(day.getDay() + 2) % 7];

In my version I also cleaned up the array to be the following:

const weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
Ledger answered 28/9, 2023 at 19:2 Comment(1)
Please, use "comment" rather, if you want to comment on another answer.Which

© 2022 - 2024 — McMap. All rights reserved.