Sunday, July 01, 2007

Some reader contributions to Calendar tutorial

When I started SZOJ I promised myself I wouldn't abruptly shut down the blog if I lost interest; I'd just go radio silent until my interest was piqued again. That said, it's been a long while. So here are two things:

Rory Parle wrote in to point out you can use Math.ceil to determine the number of rows you'd need to safely render a calendar:

Math.ceil((monthLength + startingDay) / 7);

Michiel van der Blonk suggested a way to determine the number of days in a month:

var d = new Date(nYear, nMonth + 1, 0); // nMonth is 0 thru 11
return(d.getDate());

The mechanism here is the Date constructor will attempt to convert year, month and day values to the nearest valid date. So in the above code, nMonth is incremented to find the following month, and then a Date object is created with zero days. Since this is an invalid date, the Date object defaults to the last valid day of the previous month — which is the month we're interested in to begin with. Then getDate() returns the day (1-31) of that date — voilá, we have the number of days.

Bonus: it also seems to automatically correct for leap year. It feels like a hack but it seems to work consistently.

Thanks to Rory and Michiel for their contributions.

13 Comments:

At 7:12 AM, Blogger jmakeig said...

That's the same trick we Excel nerds have been using to calculate the first day of the next month: =DATE(2007,MONTH(NOW())+1,1)-1

 
At 6:53 AM, Blogger Unknown said...

Thanks for the tip. Iv'e been looking to do this for some time.


Dan

 
At 3:46 AM, Blogger Unknown said...

Your last tutorial was brilliant! I've had a good go at it and managed to produce what you have. Just trying to work out how to have nifty little next month/previous month buttons now. :)

Thank you!

 
At 6:15 PM, Blogger Roberto Iza Valdés said...

This comment has been removed by the author.

 
At 4:03 AM, Blogger Max Payne said...

hello i am having a problem

i am using ajax to load my page i request for html page and get response in html format i want to cut specific data from html xmlHttp.open("GET","http://all-about-ngage.blogspot.com/2006/11/n-gage-games.html",true);

var resp=new String(xmlHttp.responseText);
document.getElementById("postdata").innerHTML = resp.getElementById("pstdata").innerHTML;

i am doing this but it is not working can you tell how to cut HTML between specific tag from the response

 
At 3:34 PM, Blogger Short Sale Real Estate said...

Come back!!! lol

:)

 
At 3:35 PM, Blogger Short Sale Real Estate said...

Pretty please?

:)

 
At 2:36 PM, Blogger Kor said...

Ai redescoperit roata. Intrepetorul cod al browserului face apel la calendarul intren al procesorului, care corecteaza automat data. Voila.

 
At 7:12 AM, Blogger Dive said...

thank for the tip but i can't see the 1/2 paragrapgh
garminstreetpilot

 
At 11:39 PM, Blogger Arjuna Studio said...

Very useful tips..
Thanks...

consolidation federal loan

 
At 3:25 AM, Blogger Inspiron said...

By now, you probably know that magnetic bracelets are used for hand, wrist, arm, and shoulder pain. You may also have heard that golfers and arthritis sufferers wear them, and others wear them as a preventative measure.Sorry out of topic!
http://magnetic-bracelet-clasp.blogspot.com/

 
At 4:41 AM, Blogger MaLiK said...

Hi

tq very much for ur tips

Cheers,
tandem bicycles

 
At 5:26 AM, Blogger fathaz said...

good for the tips

http://laser-hair-removal-for-men.blogspot.com/

 

Post a Comment

<< Home