Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript Problem

Options
  • 10-07-2007 8:09pm
    #1
    Registered Users Posts: 7,041 ✭✭✭


    I having a problem with a javascript piece. Its basic objective is, using todays date and day, find out what day the first of the month was. It works like this:

    Today is the 10th and a Tuesday. The days are 0=Sunday, 1=Monday etc. To find what day the first of the month was I must subtract 1 from 10 and then subtract the answer of that, 9, from the day, 2. However every time the day reaches minus 1 (or less than 0) is must reset to 6 (which is Sat) and continue subtracting. It shall then land on the answer. So,

    10-1=9. 9-2=0 (because it reset). 0=Sunday which the first of the month was. Heres the loop I'm using:
    while(date-1 > 0) {
    	if(day>0) {
    		date--;
    		day--;
    	}else{
    		day = 6;
    }
    }
    

    For some reason it comes up 6 and continues to run till the browser stops it. Anyone willing to lend a hand?

    Thanks,
    S.


Comments

  • Closed Accounts Posts: 34 little_sheep


    Seachmall wrote:
    I having a problem with a javascript piece. Its basic objective is, using todays date and day, find out what day the first of the month was. It works like this:

    Today is the 10th and a Tuesday. The days are 0=Sunday, 1=Monday etc. To find what day the first of the month was I must subtract 1 from 10 and then subtract the answer of that, 9, from the day, 2. However every time the day reaches minus 1 (or less than 0) is must reset to 6 (which is Sat) and continue subtracting. It shall then land on the answer. So,

    10-1=9. 9-2=0 (because it reset). 0=Sunday which the first of the month was. Heres the loop I'm using:
    while(date-1 > 0) {
    	if(day>0) {
    		date--;
    		day--;
    	}else{
    		day = 6;
    }
    }
    

    For some reason it comes up 6 and continues to run till the browser stops it. Anyone willing to lend a hand?

    Thanks,
    S.

    try to display date and day for each while. Javascript has some problème with number and string maybe it's this case.


  • Registered Users Posts: 1,393 ✭✭✭Inspector Gadget


    Off the top of my head, here's a quick and dirty way to do it...
    seconds_per_day = 86400;
    today = new Date();
    
    first_of_month = today - (today.getDate() * seconds_per_day);
    result = first_of_month.getDay();
    

    Haven't tested this, but the theory should be fine.
    Gadget.


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Little_Sheep,
    I threw an alert in the loop to tell me what was happening and everytime it was resetting back to 6 in didn't deduct 1 from date so the additional 1s were adding up. I put a date-- in the if and it works fine. Thanks for the tip.

    Thanks anyway gadget.


  • Registered Users Posts: 273 ✭✭stipey


    This is untested... but I think it should work.
    var dayNumber;
    var first = new Date();         [COLOR="DarkGreen"]// set to today's date by default.[/COLOR]
    first.setDate(1);               [COLOR="DarkGreen"]// first day of the month.[/COLOR]
    dayNumber = first.getDay();
    

    Feel free to shoot holes in it....


Advertisement