I can’t speak about what I do in office. (ok, not yet). But I need to write something. So here goes:
A “small” / compact algo to retrieve number of days in a month. Don’t ask me it s efficiency (modulus is never fast , an alternative would be something similar to isInt function ):
/// January = 1, February = 2 and so on.
/// year will be used only if month = 2
function daysInTheMonth(var monthNumber, var yearNumber)
{
// if February,
// and year is divisible by 4 but not divisible by 100, return 28.
// Else return 29.
// since years which are divisible by 100 should not be leap years
if(monthNumber == 2) {
return (yearNumber%4 ==0 && yearNumber%100>0)? 29 : 28
}
return (monthNumber > 7) ? ((monthNumber%2 ==0) ? 30 : 31) : ((monthNumber%2 ==0) ? 31: 30);
// another option
// if(monthNumber > 7) monthNumber++;
// return ((monthNumber%2 ==0) ? 30 : 31)
}
Comments powered by Talkyard.