VBScript Date() Function Returns Two-Digit Year for Dates Past 2000

ID: Q222178


The information in this article applies to:


SYMPTOMS

The Date() function in Microsoft Visual Basic Script (VBS) always returns a two-digit year for dates past the year 2000.


CAUSE

This is by design.

Visual Basic Script (VBS) includes several functions for manipulating dates, and the Date() function always returns a "short date." In the U.S., it is typically in mm/dd/yy format.

Although dates returned by this function only display a two-digit year, the date value used internally by VBS uses the full year value. Therefore, using a combination of functions such as Year(Date()) will return the correct year.


WORKAROUND

The following Active Server Pages (ASP) code will display several possible date output formats using built-in VBS functions, as well as defining two user-defined functions named FormatYear() and LongYear() that will return a short date with a four-digit year, or in mm/dd/yyyy format.

To use this example, copy the ASP code and save it as "DateTest.asp" (without the quotation marks) in a directory or virtual directory with Script access enabled. When you browse the page, it will display a table with several code samples for manipulating dates.


<%@Language="VBSCRIPT"%>
<html>
<head><title>VBScript Date Examples</title></head>
<%
  Function FormatYear(tmpDate)
    FormatYear = Month(tmpDate) & "/" & Day(tmpDate) & "/" & Year(tmpDate)
  End Function

  Function LongYear()
    LongYear = Month(Date()) & "/" & Day(Date()) & "/" & Year(Date())
  End Function
%>
<body>

<h2>VBScript Date Examples</h2>

<h3> Using built-in functions</h3>

<table border="1">
  <tr>
    <th colspan="2">Using Date()</th>
    <th colspan="2">Using Now()</th>
  </tr>
  <tr>
    <td nowrap>Date()</td>
    <td nowrap><%=Date()%></td>
    <td nowrap>Now()</td>
    <td nowrap><%=Now()%></td>
  </tr>
  <tr>
    <td nowrap>Year(Date())</td>
    <td nowrap><%=Year(Date())%></td>
    <td nowrap>Year(Now())</td>
    <td nowrap><%=Year(Now())%></td>
  </tr>
  <tr>
    <td nowrap>DatePart("yyyy",Date())</td>
    <td nowrap><%=DatePart("yyyy",Date())%></td>
    <td nowrap>DatePart("yyyy",Now())</td>
    <td nowrap><%=DatePart("yyyy",Now())%></td>
  </tr>
  <tr>
    <td nowrap>FormatDateTime(Date())</td>
    <td nowrap><%=FormatDateTime(Date())%></td>
    <td nowrap>FormatDateTime(Now())</td>
    <td nowrap><%=FormatDateTime(Now())%></td>
  </tr>
  <tr>
    <td nowrap>FormatDateTime(Date(),vbShortDate)</td>
    <td nowrap><%=FormatDateTime(Date(),vbShortDate)%></td>
    <td nowrap>FormatDateTime(Now(),vbShortDate)</td>
    <td nowrap><%=FormatDateTime(Now(),vbShortDate)%></td>
  </tr>
  <tr>
    <td nowrap>FormatDateTime(Date(),vbLongDate)</td>
    <td nowrap><%=FormatDateTime(Date(),vbLongDate)%></td>
    <td nowrap>FormatDateTime(Now(),vbLongDate)</td>
    <td nowrap><%=FormatDateTime(Now(),vbLongDate)%></td>
  </tr>
  <tr>
    <td nowrap>Right(FormatDateTime(Date(),vbLongDate),4)</td>
    <td nowrap><%=Right(FormatDateTime(Date(),vbLongDate),4)%></td>
    <td nowrap>Right(FormatDateTime(Now(),vbLongDate),4)</td>
    <td nowrap><%=Right(FormatDateTime(Now(),vbLongDate),4)%></td>
  </tr>
</table>

<h3> Using user-defined functions</h3>

<table border="1">
  <tr>
    <td nowrap>LongYear()</td>
    <td nowrap><%=LongYear()%></td>
  </tr>
  <tr>
    <td nowrap>FormatYear(Date())</td>
    <td nowrap><%=FormatYear(Date())%></td>
  </tr>
  <tr>
    <td nowrap>FormatYear(Now())</td>
    <td nowrap><%=FormatYear(Now())%></td>
  </tr>
</table>

</body>
</html> 


MORE INFORMATION

Information on Microsoft and Year 2000 issues can be found in the Microsoft Year 2000 Resource Center.

Information on Microsoft Scripting Technologies can be found on the Microsoft Scripting Technologies Web site.

Additional query words: Y2K VBS ASP


Keywords          : 
Version           : winnt:3.0,4.0
Platform          : winnt 
Issue type        : 

Last Reviewed: July 21, 1999