Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Last Day of Month

Status
Not open for further replies.

wf

Programmer
Feb 1, 2001
32
US
I need a function where I send a year and a month (The year for february - leap year) and it returns me the last day of the month. Does anyone has any?
 
Try using the javascript date object:

function fnDate_LastDay(i_nMonth, i_nYear) {

//construct a date, 1st of the NEXT month
// the month parameter is 0-11!
var dt = new Date(i_nYear, i_nMonth, 1);
//subtract 1 day from date
//to get last day of previous month...
dt.setDate(-1);
return dt.getDate() + 1; //getDate returns 0-30!
}

or use the dateadd function in vb script:

dim dt
dt = DateSerial (i_nYear, i_nMonth + 1, 1)
'can cope with month 13!
DateAdd("d",-1,dt)
fnDate_LastDay = Day(dt)

These may be slightly slower than typical code - but it is much more accurate (for those odd leap years):

function getNoOfDaysInMnth(i_nMnth, i_nYr) {
/*
** Get the number of days in the month based on the year.
** [Rather simplistic function, as it ignores some special cases]
*/

var rem = i_nYr % 4; //use modulo 4 for leap year
var leap = 0;
var noDays=0;

if (rem ==0)
leap = 1;

if ( (i_nMnth == 4) || (i_nMnth == 6) || (i_nMnth == 9) ||
(i_nMnth == 11)) {
noDays=30;
} else if (i_nMnth == 2) {
noDays=28+leap;
} else {
noDays=31;
}
return noDays;
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top