Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Javascript Problem

  • 10-07-2007 08: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