jfreeman2010
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/yyy y")#">
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,
<!---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",-
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,
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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,
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,
ASKER
I got this far:
<cfset dtThisMonth = CreateDate(
Year( Now() ),
Month( Now() ),
1
) />
<cfset dtLastDay = (
DateAdd( "m", 1, dtThisMonth ) -
1
) />
<cfoutput>#DateFormat(dtLa stDay, "mm/dd/yyyy")#</cfoutput>
the above get me: 6/30/2011, where should I subtract 1 to get the last month?
thanks,
<cfset dtThisMonth = CreateDate(
Year( Now() ),
Month( Now() ),
1
) />
<cfset dtLastDay = (
DateAdd( "m", 1, dtThisMonth ) -
1
) />
<cfoutput>#DateFormat(dtLa
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#" >
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Take _agx_ version.
It's not even mine anyway ;) Just a slight mod to what SidFishes posted.
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.
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.
ASKER
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 ;)
>>SideFishes snippet stated only that first of month is allways 1.<<
No he also posted the tutorial which was the source of my code ;)
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 :)