Link to home
Start Free TrialLog in
Avatar of jasch2244
jasch2244

asked on

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

Avatar of Jones911
Jones911

eq not =  

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

Open in new window

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

What is the error?
Avatar of jasch2244

ASKER

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
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>
forget the case do

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

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


<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

ASKER CERTIFIED SOLUTION
Avatar of Jones911
Jones911

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