Mark Voss

First and last day of the month

First and last day of the month

Using the VBScript DateSerial function, returning the first day of the month is straightforward:

<% first_day = DateSerial(Year(Date), Month(Date), 1) %>

Returning the the last day of the month is the same as returning the day before the first day of next month:

<% last_day = DateSerial(Year(Date), Month(Date)+1, 1 - 1) %>

Combining these into a sub procedure:

<%
Sub FirstLastDayOfMonth()

  first_day = DateSerial(Year(Date) ,Month(Date) ,1)
  last_day = DateSerial(Year(Date), Month(Date)+1 ,1 - 1) 	  

  Response.Write "The first day of the month is: " & first_day & "</p><p>"
  Response.Write "The last day of the month is: " & last_day

End Sub
%>
<p><% Call FirstLastDayOfMonth %></p>

The resulting output is:

This will work for any month – even February in a leap year!

Leave a Reply

Your e-mail address will not be published. Required fields are marked *