• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2007
  • 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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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