Tag Archives: number of days

JavaScript: Calculate the Number of Days in a Month CodeUnit 02 SEP 2011

A simple way to calculate how many days are in a month in Javascript is to leverage Javascript’s built in date overflow feature – basically if you give it an incorrect date it automatically adjusts it to the correct one by assuming that the overflow are days which need simply to be added to the given date.

So, given a month and a year, it is a simply matter of forcing a date to overflow by a certain amount and then by subtracting the result from that overflow amount to learn how many days are in that month.

For example, to calculate the number of days in the current month:

var time=new Date();
var numberOfDays = 32 - new Date(time.getFullYear (), time.getMonth(), 32).getDate();

Nifty.

JavaScript: Calculate the Number of Days in Last Month and Set a Date Range for it CodeUnit 01 JUL 2011

A simple way to calculate how many days are in a month in Javascript is to leverage Javascript’s built in date overflow feature – basically if you give it an incorrect date it automatically adjusts it to the correct one by assuming that the overflow are days which need simply to be added to the given date.

So, given a month and a year, it is a simply matter of forcing a date to overflow by a certain amount and then by subtracting the result from that overflow amount to learn how many days are in that month.

Thus by setting the start marker to one month back in time, we can easily learn how many days were in the last month, making it a snap to provide a quick last month range pick for a calendar control.

var time=new Date();
//force the date object to last month
var currentMonth = time.getMonth();
time.setMonth(time.getMonth()-1);
while(currentMonth == time.getMonth()){
    time.setMonth(time.getMonth()-1);
}
//get the last day of last month
var lastDay = 32 - new Date(time.getFullYear (), time.getMonth(), 32).getDate();
//if we were setting a last month range for a calendar control
var monthstart = new Date(time.getFullYear (),time.getMonth(),1);
var monthend = new Date(time.getFullYear (),time.getMonth(),lastDay);

Nifty.

PHP: Calculate the Number of Working Days in a Month CodeUnit 23 APR 2010

How to calculate the number of working days in a month, in other words discount Saturdays and Sundays from a month’s total number of days.

It sounds pretty simple, but because of the shifting nature of the calendar we use, one can’t just take a guess and average it you know. No, we need a function to work out this total for each month of any particular year.

Now of course I’m sure there are for more acute, beautiful or elegant solutions to this particular problem out there, but I for one quite like the idea of seeing the logic behind what is happening step by step, which is exactly why I came up with this little function to do the work for me.

So in order to calculate the number of working days in a month by removing Saturdays and Sundays from its day count, have a look at this simple PHP function:

function calculateWorkingDaysInMonth($year = '', $month = '')
{
	//in case no values are passed to the function, use the current month and year
	if ($year == '')
	{
		$year = date('Y');
	}
	if ($month == '')
	{
		$month = date('m');
	}	
	//create a start and an end datetime value based on the input year 
	$startdate = strtotime($year . '-' . $month . '-01');
	$enddate = strtotime('+' . (date('t',$startdate) - 1). ' days',$startdate);
	$currentdate = $startdate;
	//get the total number of days in the month	
	$return = intval((date('t',$startdate)),10);
	//loop through the dates, from the start date to the end date
	while ($currentdate <= $enddate)
	{
		//if you encounter a Saturday or Sunday, remove from the total days count
		if ((date('D',$currentdate) == 'Sat') || (date('D',$currentdate) == 'Sun'))
		{
			$return = $return - 1;
		}
		$currentdate = strtotime('+1 day', $currentdate);
	} //end date walk loop
	//return the number of working days
	return $return;
}

As you can see, the logic of the function is pretty straightforward to follow. To use the function in your code, simply call it and pass the month and year you want it to examine, meaning that

calculateWorkingDaysInMonth(2010,04);

is going to result in 22.

Nice! :)