Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show previous month 1

Status
Not open for further replies.

faver

MIS
Apr 5, 2007
11
US

hello,

I am writing a script and I need to show the previous month

I can show the current month but I have no idea how to go about showing the previous month could somebody point me in the right direcction?

Here is what I have for the current month

Code:
<% CurrDate = Now()
CurrMonthID = Month(CurrDate)
CurrMonthName = MonthName(CurrMonthID)
response.write "" & CurrMonthName %>
 

Month() gives you the current month number.. e.g. Jan=1, feb=2 etc.

so to show the previous month simply subtract 1 and perform a bit of logic to deal with moving from Jan to Dec (1-->12)

e.g.

Code:
<% 
CurrMonthNo = Month(Now())
if CurrMonthNo = 1 then
 currMonthNo = 12 
else
 currMonthNo = currMonthNo -1
end if
CurrMonthName = MonthName(CurrMonthNo)
response.write "" & CurrMonthName %>



A smile is worth a thousand kind words. So smile, it's easy! :)
 
A slightly shorter method of managing this would be to simply subtract a month from now and use that as your starting point:
Code:
PrevMonth = DateAdd("m",-1,Now())
PrevMonthNo = Month(PrevMonth)
Response.Write MonthName(PrevMonthNo) & " is month #" & PrevMonthNo

Not necessarily more efficient, but a little more compact and might be a little easier to figure out 6 months later when you go to make a change.

-T

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top