Link to home
Start Free TrialLog in
Avatar of jfreeman2010
jfreeman2010Flag for United States of America

asked on

coldfusion first and last day of previous month

Hi, I need to have a cfparam date default as first day of previous month and last day of previous month.  Here is what I had and it did not work:

<!---First Day of Previous Month--->
<cfparam name="url.StartDate" default = "#DateFormat(DATEADD("m", DATEDIFF("m", 0, now()) - 1, 0),"mm/dd/yyyy")#">
 
<!---Last day of Previous Month--->
<cfparam name="url.EndDate" default = "#DateFormat(DATEADD("d",-1,DATEADD("m", DATEDIFF("m",0,now()),0)),"mm/dd/yyyy")#">

the result of the url.startdate = 4/30/2011
and the url.enddate = 05/29/2011.

What I am looking is the startdate = 05/01/2011 and enddate=05/31/2011

thank you,
Avatar of SidFishes
SidFishes
Flag of Canada image

Ben's got a function & tut here to get last day of month

http://www.bennadel.com/blog/882-Ask-Ben-Finding-The-Last-Monday-Of-The-Month-In-ColdFusion.htm

and to get first day = well it's always 1 isn't it :)
SOLUTION
Avatar of SidFishes
SidFishes
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jfreeman2010

ASKER

Hi SidFishes,

Thank you very much for the help.  I did try to understand Ben's function, but unable to got it to work for the last day of the month.  Can you please copy/past it out which part I need to get the last day of the month.

I do understand your code for the first day of the month and substract 1 for the previous month logic.  thank you,
I got this far:

<cfset dtThisMonth = CreateDate(
Year( Now() ),
Month( Now() ),
1
) />

<cfset dtLastDay = (
DateAdd( "m", 1, dtThisMonth ) -
1
) />
 
 
<cfoutput>#DateFormat(dtLastDay, "mm/dd/yyyy")#</cfoutput>


the above get me:  6/30/2011, where should I subtract 1 to get the last month?

thanks,
How about this:
<!---First Day of Previous Month--->
<cfparam name="url.StartDate" default = "#DateFormat(CreateDate(Year(Now()),Month(Now())-1,1),"mm/dd/yyyy")#">
 
 
<!---Last day of Previous Month--->
<cfparam name="url.EndDate" default = "#DateFormat(DateAdd("d",-1,CreateDate(Year(Now()),Month(Now()),1)),"mm/dd/yyyy")#">

<cfdump var="#URL#" >

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Take _agx_ version.

It's not even mine anyway ;) Just a slight mod to what SidFishes posted.
"a slight mod"!!!
SideFishes snippet stated only that first of month is allways 1.
His yyyy calculation and zero for January are simply wrong.

ya that January thing, missed that.
thank you all!!!
>"a slight mod"!!!<
>>SideFishes snippet stated only that first of month is allways 1.<<
No he also posted the tutorial which was the source of my code ;)