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!

Working with Months and Years 1

Status
Not open for further replies.

JohnBoy2005

Programmer
Jul 12, 2005
117
GB
How would I go about writing a routine that would list the next 18 months.

May 07
Jun 07
Jul 07
etc...

Cheers

John
 
Code:
today = now
mm = month(today)
yy = year(today)
for ii = 1 to 18
   WScript.Echo MonthName(mm, true) & " " & cstr(yy)
   mm = mm + 1
   if mm > 12 then
      mm = 1
      yy = yy + 1
   end if
next
 
Rather than manually increment through the month and year, why not allow the DateAdd function to do it for you?
Code:
Dim tdate, i
tdate = Now()
For i = 1 to 18
   tdate = DateAdd("m",1,tdate)
   Response.Write MonthName(Month(tdate)) & " " & Year(tdate) & "<br/>"
Next
The downside of this function is that it is probably a little less efficient (a call ot Month() and Year() in every loop instead of using a variable for each), but it is more compact without losing it's readability.
-T

 
Why not use DateSerial and Format?

Code:
_Year = Year(date())
_Month = Month(date())
For i = 0 to 17
  strMonth = Format(DateSerial(_Year, (_Month + i), 1), "mmm yyyy")
Next
 
Unfortunately Format is not a function in VBScript.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top