daysInMonth

I have a simple event calendar which was initianallly just the current month, so I had varialble firstOfMonth and endOfTheMonth declared like this:


<cfset firstOfTheMonth = createDate(year(now()), month(now()), 1)>
<cfset endOfTheMonth = createdatetime(year(now()), month(now()), #daysinmonth(firstOfTheMonth)#, 23, 59, 59)>

Now the site owners want to have the oportunity to have next and previous links as well, which I created the following way:


<cfif structkeyexists(url,'TheDate')>
	<cfset TheDate = createDate(year(url.TheDate), month(url.TheDate), 1)>
<cfelse>
	<cfset TheDate = createDate(year(now()), month(now()), 1)>
</cfif>

<a href="#cgi.SCRIPT_NAME#?TheDate=#dateformat(dateAdd('M',-1,TheDate),'mm/dd/yyyy')#"> Prev </a>
<a href="#cgi.SCRIPT_NAME#?TheDate=#dateformat(dateAdd('M', 1,TheDate),'mm/dd/yyyy')#"> Next </a>

And I adjusted the firstOfMonth and endOfTheMonth variables the following way:


<cfif not structKeyExists( Url, 'TheDate' )>
	<cfset firstOfTheMonth = createDate(year(now()), month(now()), 1)>
	<cfset endOfTheMonth = createdatetime(year(now()), month(now()), #daysinmonth(firstOfTheMonth)#, 23, 59, 59)>
<cfelse>
	<cfset firstOfTheMonth = createDate(year(Url.TheDate), month(Url.TheDate), 1)>
	<cfset endOfTheMonth = createdatetime(year(Url.TheDate), month(Url.TheDate), #daysinmonth(firstOfTheMonth)#, 23, 59, 59)>
</cfif>

But I must be making a thinking mistake somewhere, because although the calendar with the links is working and is show the right days for each month, I end up with 30 days for each month? What am I doing wrong.

Thank you in advance!

your mistake is thinking that the date in the url retains it’s date object status. It’s getting turned into a string and createDate and createDateTime both need date objects to work from. Below is what I use for generating a calendar, but why do you care what second the month ends on (createDate for beginning of month and createDateTime for end of month)?

Here’s what I use for a calendar


<cfif NOT StructKeyExists(URL,'month')>
     <cfparam name="SESSION.calendar_month" type="numeric" default="#Month(Now())#" max="12" min="1" />
     <cfparam name="SESSION.calendar_year" type="numeric" default="#Year(Now())#" />
<cfelse>
     <cfset SESSION.calendar_month = URL.month>
     <cfset SESSION.calendar_year = URL.year>
</cfif>
<cfset days_in_month = DaysInMonth(CreateDate(SESSION.calendar_year,SESSION.calendar_month,1))>
<cfset month_start_padding = dayOfWeek(CreateDate(SESSION.calendar_year,SESSION.calendar_month,1))>
<cfset month_end_padding = 7 - dayOfWeek(CreateDate(SESSION.calendar_year,SESSION.calendar_month,days_in_month))>
<cfset month_start = createDate(SESSION.calendar_year,SESSION.calendar_month,1)>
<cfset month_end = createDate(SESSION.calendar_year,SESSION.calendar_month,days_in_month)>