Diggetydog
Diggetydog

Reputation: 56

javascript loop through dates - stop and display correct date

I have a loop that checks the current date against the week from sunday-saturday and then check if it is past, current or due. It ALWAYS SHOWS next week!

logic is as follows

If it is the current weekday then display todays date, if it is a past weekday then display that date + 7 to get next weeks date, if it is a day coming up then display that date.

Here is the code that I have put together.

document.getElementById('youchose').innerHTML = localStorage.day;

window.myDate = new Date(); 
if(localStorage.getItem("setDate") != null && localStorage.setDate != null && localStorage.setDate!="0000-00-00" && localStorage.setDate!=""){
    a = localStorage.setDate;
    a = a.split("-");
    if(a.length >= 1){
        window.myDate= new Date(a[0],a[1]-1,a[2]);
    } else {
        window.myDate = new Date(); 
    }
}
var weekdays = new Array();
weekdays['Sunday'] = 0;
weekdays['Monday'] = 1;
weekdays['Tuesday'] = 2;
weekdays['Wednesday'] = 3;
weekdays['Thursday'] = 4;
weekdays['Friday'] = 5;
weekdays['Saturday'] = 6;

//loop starts

for(weeki=0; weeki <= 7; weeki++){
    if(window.myDate.getDay() == weekdays[localStorage.day]){ //for current day
        SelectFromDB();
        window.myDate.setDate(window.myDate.getDate());
        weeki = 100;
        break;
    }else if(window.myDate.getDay() > weekdays[localStorage.day]) { //for previous days
        window.myDate.setDate(window.myDate.getDate()-1);
    }else{
        window.myDate.setDate(window.myDate.getDate()+1); // for next days
    }
}

It won't it just keeps adding 7 to all the dates. what am I missing. been staring at this for days it seems.

Thanks for any help

Upvotes: 0

Views: 271

Answers (1)

RobG
RobG

Reputation: 147553

Not a complete answer as there are issues with just about every line of your code.

The use of window properties instead of variable declarations works, but it is probably better to use variable declarations rather than property assignments, so instead of:

window.myDate = new Date(); 

consider:

var myDate = new Date();

which has subtle but useful benefits. Similarly, accessing global variables as window.varName is just a long winded way of accessing varName and is problematic if the code is intended to be run in a non–browser host (which this doesn't appear to be, but not a good habit).

Then:

if(localStorage.getItem("setDate") != null && localStorage.setDate != null && localStorage.setDate!="0000-00-00" && localStorage.setDate!=""){

The localStorage API is very simple, it only has a couple of methods, there is no "setDate" method so the above will always return false since localStorage.setDate will return undefined which == null.

So none of the code in the if block will run.

Also there is:

var weekdays = new Array();
weekdays['Sunday'] = 0;

Arrays are just objects with a special length property. The above creates an array, then treats it as a plain object and is equivalent to:

var weekdays = new Object();
weekdays['Sunday'] = 0;

Most would code it using an object literal:

var weekdays = {Sunday: 0, Monday: 1, Tuesday: 2, Wednesday: 3,
                Thursday: 4, Friday: 5, Saturday: 6};

Presumably the value returned from local storage is something like '2016-05-19', which you then correctly parse to a Date.

Then:

if (window.myDate.getDay() > weekdays[localStorage.day]) 

Doesn't seem correct, perhaps you meant:

if (window.myDate.getDay() > weekdays[localStorage.getItem('day')])

Anyway, that is a start on your issues.

Upvotes: 1

Related Questions