http://www.chrispearson.org/pages/programming/vbscript/datework/misc/daysinamonth.asp
17h06
Thursday, 28. August 2008

DAYS

There are several ways of determining how many days there are in a given month. A couple of techniques I've used are discussed here.  

A hacker's way

Although it's generally reckoned that using functions to return a result is best - the code looks more elegant, anyway! - there can be advantages in iterative solutions.

This one uses invalid date error trapping to determine the number of days in the month:

    <%
   strMonth = " August "
   strYear = " 2008 "
   On Error Resume Next
   For i = 28 to 31
      intLastDay = Day(i & " " & strMonth & " " & strYear)
   Next
   %>

intLastDay is assigned the day number from 28 - the least number of days possible - to 31 - the maximum. Since we specify On Error Resume Next, the assignment of intLastDay is skipped for invalid dates, in this example giving 31 .

The Day() function is discussed below.

The elegant way

To return the number of days through VBScript functions we need to combine the use of three functions:

  • DateSerial()
  • DateAdd()
  • Day()

Before looking at the combination of these it's worth reviewing what each of them can do.

1   DateSerial()

DateSerial() can be used to turn elements of a date entity into a date, for instance

   intYear = 2008
   intMonth = 8
   intDay = 28
   response.write DateSerial(intYear, intMonth, intDay)

produces the output 28/08/2008 . Notice that the date format used in the output is determined by the settings on the server since no explicit format is specified for the date here.

An important point here is that, this being month , month number 8 , the following code returns the fist day of next month:

   intYear = 2008
   intThisMonth = 8
   intToday = 28         ' Doesn't matter
   response.write DateSerial (intYear, intThisMonth + 1, 1)

The output being 01/09/2008 . VBScript is clever enough to increment the year if we add 1 to month 12, as in

   response.write DateSerial( 2008 , 12 + 1, 1)

which displays 01/01/2009 , next new year's day.

So we now have a mechanism to determine the first days of the given month and the month which follows it.

2   DateAdd()

This function handles arithmetic on dates in a formal way. In this current example we will use it to calculate the day before the first day of next month: The last day of this month, that is!

The following line of code

   Response.Write DateAdd("d", -1, DateSerial( 2008 , 8 + 1, 1 ))

gives output of 31/08/2008 . It takes the date we calculated above, 01/09/2008 , and -- as we specify "d" for days and -1 to add (take one away, that is) -- the function decrements the days of the given date by one. Again, when days are manipulated, months and years are updated as necessary.

We now have a mechanism to determine the first and last days of the given month. We could use a difference between the two dates to return the number of days in the month. Since the month always starts on the first (!) and we need an inclusive count, using the Day() function is an ideal way to get the count of days.

3   Day()

The Day() function return an integer which represents the day number in the specified month, so

   Day( 28/08/2008 )

returns 28 . Correct, since today is 28 . August .

and

Day( 01/08/2008 )

returns 1 .

More importantly in this exercise:

   Response.Write "The number of days in the month is " & _
      Day(DateAdd("d", -1, DateSerial(Year(Now), Month(Now) + 1, 1)))

output the string The number of days in this month is 31 .

 
Which is what we were looking for!   But notice this . . . There is a simple way to return days in a month

xxx,xxx

copyright ©2000 - 2008 Chris Pearson