• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1935
  • Last Modified:

Getting previous month and dates

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

0
jasch2244
Asked:
jasch2244
  • 4
  • 2
  • 2
  • +1
1 Solution
 
Jones911Commented:
eq not =  

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

Open in new window

0
 
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>
0
 
PluckaCommented:
Looks ok, not sure 17-23 are necessary though.

What is the error?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
jasch2244Author 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
0
 
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>
0
 
Jones911Commented:
forget the case do

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

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

0
 
Jones911Commented:

<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

0
 
Jones911Commented:
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

0
 
jasch2244Author 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!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now