# JavaScript: Calculate the Number of Days in a MonthCodeUnit02 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 itCodeUnit01 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.

# jQueryUI Datepicker Calendar Control Visible at the Bottom of the PageCodeUnit24 JUN 2011

For some or other annoying reason, my jQueryUI datepicker controls start exhibiting a weird bug where on page load, the normally hidden control calender was appearing right at the bottom of the page. Clicking on the launch control would then bring up the calendar correctly, and on selection or unfocus would then hide it again correctly. So only on page load then.

Luckily the fix to this annoying problem is actually nothing more than a simple CSS rule which you can add to the top of the affected page. (Note that this is after I spent a fair amount of time updating all my javascript plugins and trawling through the code to try and find the problem).

`#ui-datepicker-div { display: none; }`

Yup, nothing more than that and your unwanted extra space at the bottom of the page is done for.

Nifty. (And yes, I still regard the jQuery and jQueryUI combo’s datepickers as one of the best ones currently available to website designers!)

# Get the Last Day of a Month using PHPCodeUnit27 MAR 2010

Picked this cool little PHP function to calculate and return the last day of a month for any year off the always informative lutrov interactive website the other day, and posted it here for future reference – because that’s just how I roll! ;)

Anyway, here goes:

```function lastday(\$month = '', \$year = '',\$format = 'Y-m-d') {
if (empty(\$month)) {
\$month = date('m');
}

if (empty(\$year)) {
\$year = date('Y');
}

\$result = strtotime("{\$year}-{\$month}-01");
\$result = strtotime('-1 second', strtotime('+1 month', \$result));

return date(\$format, \$result);
}```

As you can see, usage is a simple matter of calling lastday() and feeding it the year and month you want it to return the last day for, though if you leave these two parameters out, it will simply return you the last date of the current month – all in a handy ISO 8601 formatting.

Useful.