Search code examples
dateasp-classic

ASP Yesterday Date


I'm using the following code to get today's date. How can i alter this code to get yesterdays date?

<%
' NewDate
 ddate = DatePart("d",Date)
 mdate = DatePart("m",Date)
 ydate = DatePart("yyyy",Date)
 if ddate <= 9 then
 ddate = "0" & ddate
 end if
 if mdate <= 9 then
 mdate = "0" & mdate
 end if
 newdate = ydate & "-" &  mdate & "-" &  ddate
%> 

Solution

  • VBScript stores dates as a numeric value of days from an initial date. To calculate relative dates, simply add or subtract the corresponding number of integral days from the initial value.

    Add a line at the beginning with:

    Date = DateAdd("d", -1, Date) ' Alternately Date - 1
    

    Depending on your ability to control the system date formats, you could also use the FormatDateTime(Date) function to perform this in a single line. The VBScript implementation is limited and does not support the general formatting options available in other languages.