Skip to main content

Algo for getting the number of days in a month

I can’t speak about what I do in office. (ok, not yet). But I need to write something. So here goes:

 

 

A “small” 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 28.

                // 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

Popular posts from this blog

So you have your website deployed in PROD ... now what ??

Posting on behalf of Usr.Web.Speed - My previous job had been to architect and develop websites for various customers. During that time my team and I have architected and developed various web applications mainly for enterprises. (But below info is not restricted to enterprises) Other than the usual development and testing tasks involved, our focus area was to abide by multiple SLAs. One of the primary SLAs was to provide the users of our websites a very low (usually subsecond) response time (or page load time). To adhere to this SLA, we did multiple activities, in code, process as well as infrastructure. These include (but not limit to) - Using best practices including (http://developer.yahoo.com/performance/rules.html) Determining the optimum number of calls to the databases, open connections, etc. Providing the fastest mechanisms to download associated content (such as stylesheets, JS files, etc. over CDN) And debugging the reason for the slowness of the websites, when ...