previous month year repost

# Thread: previous month year repost

1. Senior Member
Join Date
Dec 1969
Posts
151

## previous month year repost

I added this message yesterday and some guy told me that answer is in FAQ..but its not...here is question again<BR><BR>I have month in a variable and year in a variable now i need to get the previous month and year 1.e, if the month i have is 9 and year is 2003 then it must display month as 8 and year as 2003 and again if i have month as 1 and year as 2004 then it must display month as 12 and year as 2003 <BR>..i dunno how to go about this.I you can help me.i would i appreciate it <BR>thanks

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## Ohh come on!

Lastmonthyear = Month(Dateadd("m", -1, Date()) & " " & Year("m", -1, Date()) <BR>

3. Senior Member
Join Date
Dec 1969
Posts
5,955

## Easy peasy...

...in JavaScript:<BR><BR>var nMonth = 9;<BR>var nYear = 2003;<BR>var nPreviousMonth = ((nMonth - 1 + 12) - 1) % 12 + 1;<BR>var nPreviousYear = (nMonth == 1?nYear - 1:nYear);<BR><BR>In VBScript:<BR><BR>Dim nMonth, nYear, nPreviousMonth, nPreviousYear<BR><BR>nMonth = 9<BR>nYear = 2003<BR>nPreviousMonth = ((nMonth - 1 + 12) - 1) Mod 12 + 1<BR>nPreviousYear = nYear<BR>If nMonth = 1 Then<BR>&nbsp;&nbsp;nPreviousYear = nYear - 1<BR>End If<BR><BR>Oliver.<BR>

4. Senior Member
Join Date
Dec 1969
Posts
11,247

## LOL even Easier peasier...

In VB anyhow see mine!

5. Senior Member
Join Date
Dec 1969
Posts
151

## RE: Ohh come on!

thanks

6. Senior Member
Join Date
Dec 1969
Posts
1,846

## Some of the info is in the FAQ

http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=48<BR>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctdatepart.asp<BR><BR>Example:<BR><BR>TheMonth = "9"<BR>TheYear = "2003"<BR><BR>&#039;Build Dummy date (use the first of the month)<BR>TempDate = TheMonth & "/1/" & 2003<BR>TempDate = DateAdd("m", -1, TempDate)<BR><BR>NewMonth = DatePart("m",TempDate")<BR>NewYear = DatePart("yyyy",TempDate)<BR>

7. Senior Member
Join Date
Dec 1969
Posts
1,846

## oops correction

Example: <BR><BR>TheMonth = "9" <BR>TheYear = "2003" <BR><BR>&#039;Build Dummy date (use the first of the month) <BR>TempDate = TheMonth & "/1/" & TheYear<BR>TempDate = DateAdd("m", -1, TempDate) <BR><BR>NewMonth = DatePart("m",TempDate") <BR>NewYear = DatePart("yyyy",TempDate)

8. Senior Member
Join Date
Dec 1969
Posts
11,247

## I'm curious

Why do you even need to build a dummy date?<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
1,846

## Well I assumed (maybe I'm wrong)

that you needed a month day and year to have a valid date in order to take a month off of. Maybe:<BR><BR>9/2003 is a valid date? the poster made no mention of having a day. He/she said they had day and year in variables.

10. Senior Member
Join Date
Dec 1969
Posts
5,955

## I know. I was going down...

...that route to start with but then thought - no, this is too simple. Lets confuse things. ;-)<BR><BR>Oliver.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•