We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Getting previous month and dates

Medium Priority
2,182 Views
Last Modified: 2013-12-20
I recently got a peice of code off of experts exchange (see code) but it's throwing an error on my end. I'm not familiar with mod but I know the error has to do with the mod portion of the code. The error message says it's looking at the = .

Can someone help with the code and explain why it's throwing an error?
<cfset lcdate=now()>
 
<!--- previous date - 1month --->
<cfset prevdate = dateadd("m",-1,lcdate)> 
 
<!--- previous month --->
<cfset prevmonth= month(prevdate)>
 
<!--- previous year --->
<cfset prevyear = year(prevdate)>
 
<!--- previous default last day of previous date --->
<cfset prevlastday=31 >
 
<!--- change previous last day depending on month --->
<cfswitch expression="#prevmonth#">
<cfcase value=2>
      <cfif prevyear mod 4 = 0>
      <cfset prevlastday= '29'>
      <cfelse>
      <cfset prevlastday= '28'>
      </cfif>
</cfcase>
<cfdefaultcase>
<cfset prevlastday= '30'>
</cfdefaultcase>
</cfswitch>
 
<cfset fromdate=createdate(prevyear,prevmonth,1)>
<cfset lastdate=createdate(prevyear,prevmonth,prevlastday)>
 
<cfoutput> #fromdate# to #lastdate#</cfoutput>

Open in new window

Comment
Watch Question

Commented:
eq not =  

In cf = is the assign operator
<cfif prevyear mod 4 eq 0>

Open in new window

erikTsomikSystem Architect, CF programmer

Commented:
all you need to do is to change = to eq

look below
<cfset lcdate=now()>
 
<!--- previous date - 1month --->
<cfset prevdate = dateadd("m",-1,lcdate)>
 
<!--- previous month --->
<cfset prevmonth= month(prevdate)>
 
<!--- previous year --->
<cfset prevyear = year(prevdate)>
 
<!--- previous default last day of previous date --->
<cfset prevlastday=31 >
 
<!--- change previous last day depending on month --->
<cfswitch expression="#prevmonth#">
<cfcase value=2>
      <cfif prevyear mod 4 eq 0>
      <cfset prevlastday= '29'>
      <cfelse>
      <cfset prevlastday= '28'>
      </cfif>
</cfcase>
<cfdefaultcase>
<cfset prevlastday= '30'>
</cfdefaultcase>
</cfswitch>
 
<cfset fromdate=createdate(prevyear,prevmonth,1)>
<cfset lastdate=createdate(prevyear,prevmonth,prevlastday)>
 
<cfoutput> #fromdate# to #lastdate#</cfoutput>

Commented:
Looks ok, not sure 17-23 are necessary though.

What is the error?

Author

Commented:
erikTsomik: you were correct with the eq vs = but for some reason the switch statement does not evaluate correctly. It has the last day of last month (Jan) as 30 when it should be 31st
erikTsomikSystem Architect, CF programmer

Commented:
sorry change to this
<cfset lcdate=now()>
 
<!--- previous date - 1month --->
<cfset prevdate = dateadd("m",-1,lcdate)>
 
<!--- previous month --->
<cfset prevmonth= month(prevdate)>
 
<!--- previous year --->
<cfset prevyear = year(prevdate)>
 
<!--- previous default last day of previous date --->
<cfset prevlastday=31 >
 
<!--- change previous last day depending on month --->
<cfswitch expression="#prevmonth#">
<cfcase value=2>
      <cfif prevyear mod 4 eq 0>
      <cfset prevlastday= '29'>
      <cfelse>
      <cfset prevlastday= '28'>
      </cfif>
</cfcase>
<cfdefaultcase>
<cfset prevlastday= '31'>
</cfdefaultcase>
</cfswitch>
 
<cfset fromdate=createdate(prevyear,prevmonth,1)>
<cfset lastdate=createdate(prevyear,prevmonth,prevlastday)>
 
<cfoutput> #fromdate# to #lastdate#</cfoutput>

Commented:
forget the case do

<cfset prevlastday = daysInMonth(month(prevdate)) />

http://cfquickdocs.com/cf8/#DaysInMonth

Commented:

<cfset lcdate=now()>
 
<!--- previous date - 1month --->
<cfset prevdate = dateadd("m",-1,lcdate)> 
 
<!--- previous month --->
<cfset prevmonth= month(prevdate)>
 
<!--- previous year --->
<cfset prevyear = year(prevdate)>
 
<!--- change previous last day depending on month --->
<cfset prevlastday = daysInMonth(month(prevdate)) />
 
<cfset fromdate=createdate(prevyear,prevmonth,1)>
<cfset lastdate=createdate(prevyear,prevmonth,prevlastday)>
 
<cfoutput> #fromdate# to #lastdate#</cfoutput>

Open in new window

Commented:
Whoops no need for the month.
<cfset lcdate=now()>
 
<!--- previous date - 1month --->
<cfset prevdate = dateadd("m",-1,lcdate)> 
 
<!--- previous month --->
<cfset prevmonth= month(prevdate)>
 
<!--- previous year --->
<cfset prevyear = year(prevdate)>
 
<!--- change previous last day depending on month --->
<cfset prevlastday = daysInMonth(prevdate) />
 
<cfset fromdate=createdate(prevyear,prevmonth,1)>
<cfset lastdate=createdate(prevyear,prevmonth,prevlastday)>
 
<cfoutput> #fromdate# to #lastdate#</cfoutput>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Jones911 thanks for working it out on me. Looking back I realized I asked the question in the wrong way. I should have asked for a complete work through which you did v.s the eq = job. Thanks a bunch. If I could award more points I'd do so!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.