tags:

views:

416

answers:

10
+4  A: 

If the month isn't February, get the number from the array. Otherwise, check if the year is leap to return 29, or return 28. Is there a problem with that?

Moayad Mardini
+2  A: 

I'm mostly agreeing w/ Moayad. I'd use a table lookup, with an if check on February and the year.

pseudocode:

Last_Day = Last_Day_Of_Month[Month];
Last_Day += (Month == February && Leap_Year(Year)) ? 1 : 0;

Note that Leap_Year() can't be implemented simply as (Year % 4 == 0), because the rules for leap years are way more complex than that. Here's an algorithm cribbed from Wikipedia

bool Leap_Year (int year) {
   return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
T.E.D.
+1  A: 
function caldays(m,y)
{

        if(m==01||m==03||m==05||m==07||m==08||m==10||m==12)
    {
     var dmax = 31;   
     return dmax;         

    }
    else if (m==04||m==06||m==09||m==11)
    {

            var dmax = 30;  
     return dmax;    

    }
    else
    {

     if((y%400==0) || (y%400==0 && y%100!=0))
     {

      dmax = 29;   
      return dmax;

     }
                else 
                {
                    dmax = 28;     
                }
     return dmax;

    }

}

source: http://www.dotnetspider.com/resources/20979-Javascript-code-get-number-days-perticuler-month-year.aspx

JuanZe
+1  A: 

I agree with Moayad and TED. Stick with the lookup table unless the month is February. If you need an algorithm for checking leap years, wikipedia has two:

if year modulo 400 is 0 then leap
 else if year modulo 100 is 0 then no_leap
 else if year modulo 4 is 0 then leap
 else no_leap

A more direct algorithm (terms may be grouped either way):

function isLeapYear (year):
 if ((year modulo 4 is 0) and (year modulo 100 is not 0)) or (year modulo 400 is 0)
  then true
 else false
Simon P Stevens
+7  A: 

I've been doing this using the Date object (assuming it's compiled, and hence blindingly fast compared to scripting).

The trick is that if you enter a too high number for the date part, the Date object wraps over into the next month. So:

var year = 2009;
var month = 1;
var date = 29;

var presumedDate = new Date(year, month, date);

if (presumedDate.getDate() != date)
    WScript.Echo("Invalid date");
else
    WScript.Echo("Valid date");

This will echo "Invalid date" because presumedDate is actually March 1st.

This leaves all the trouble of leap years etc to the Date object, where I don't have to worry about it.

Neat trick, eh? Dirty, but that's scripting for you...

Tor Haugen
This looks like the best solution but is Date expected to do that in all implementation by ECMA standard?
the_drow
nice trick, and it's definitely a good idea to build on and use established things like the Date class, however I just did some benchmarks and this is significantly slower (+650%) than the method I proposed!
nickf
+1 Really nice trick, Tor :)
roosteronacid
+1 for the trick. Although it's not very friendly, I wouldn't be able to understand it without your explanation!
Moayad Mardini
+14  A: 
function daysInMonth(m, y) { // m is 0 indexed: 0-11
    switch (m) {
        case 1 :
            return (y % 4 == 0 && y % 100) || y % 400 == 0 ? 29 : 28;
        case 8 : case 3 : case 5 : case 10 :
            return 30;
        default :
            return 31
    }
}

function isValid(d, m, y) {
    return m >= 0 && m < 12 && d > 0 && d <= daysInMonth(m, y);
}
nickf
I think this is really cooL! Great!
Aviator
The comment says that m is zero-indexed, but still your switch relies on the months being one-indexed; 1: January, 2: February, etc.
roosteronacid
+1 for a great code!
Moayad Mardini
And.. Nice work! Not that this was a pop-quiz, but you get an A+ :)
roosteronacid
Actually, it's flawed. If m is 0-indexed (as the comment states), March has 28 or 29 days...
Tor Haugen
There, fixed that for ya'.
paxdiablo
ah thanks Pax - yeah brainfart on february there.. the other ones were good though! ;)
nickf
historical note: that's the gregorian leap year formula. wikipedia "gregorian calendar". qoute: "The last day of the Julian calendar was Thursday, 4 October 1582 and this was followed by the first day of the Gregorian calendar, Friday, 15 October 1582 (the cycle of weekdays was not affected)." also: "the British Empire (including the eastern part of what is now the United States) adopted the Gregorian calendar in 1752 by which time it was necessary to correct by 11 days. Wednesday, 2 September 1752 was followed by Thursday, 14 September 1752". good thing noone cares about dates that old?
ifatree
roosteronacid
@rooster, I thought about that, but the only performance gain you'll get will be on those years which are divisible by 400, so unless you're working with a lot of dates in the year 2000 (or 2400), then no.
nickf
Thanks, nickf. I am typically working with dates ranging from 2000 till 2010 (current year), so I guess I'll switch it round.
roosteronacid
2010, current year? Can you like, email me lotto results or something??
nickf
+2  A: 

This will not perform as well as the accepted answer. I threw this in here because I think it is the simplest code. Most people would not need to optimize this function.

function validateDaysInMonth(year, month, day)
{
    if (day < 1 || day > 31 || (new Date(year, month, day)).getMonth() != month)
        throw new Error("Frack!");
}

It takes advantage of the fact that the javascript Date constructor will perform date arithmetic on dates that are out of range, e.g., if you do:

var year = 2001; //not a leap year!
var month = 1 //February
var day = 29; //not a valid date for this year
new Date(year, month, day);

the object will return Mar 1st, 2001 as the date.

RedFilter
+1  A: 

Assuming the JS Date object standard where months are numbered from 0, and you have your daysInMonth array:

var days = daysInMonth[month] + ((month === 1) && (year % 4 === 0) && ((year % 100 !== 0) || (year % 400 === 0)));

will give you the number of days in the month, with 28 increased to 29 iff the month is February and the year is a leap year.

NickFitz
+1  A: 

all this logic is already built in to the javascript engine... Why recode it ? Unless you are doing this just as an exercise, you can use the javascript Date object:

Like this:

function daysInMonth(aDate) {
      return new Date(aDate.getYear(), aDate.getMonth()+1, 0).getDate();      
   }
Charles Bretana
+1  A: 

You can use DateTime to solve this:

new DateTime('20090901')->format('t'); // gives the days of the month
Mobbit
Is this javascript ?
Charles Bretana