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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Javascript Problem

  • 10-07-2007 7:09pm
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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