0

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
%> 
user3501463
  • 55
  • 2
  • 9

1 Answers1

2

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.

Pekka
  • 3,529
  • 27
  • 45
  • 1
    I wouldn't recommend this, stick to using the in-built functions like [`DateAdd()`](http://msdn.microsoft.com/en-us/library/cb7z8yf9(v=vs.84).aspx) to manipulate dates in VBScript that's what they're there for. Also *"The VBScript implementation is limited"* is a misnomer, it is only as limited as you make it. For information on formatting VBScript Dates see http://stackoverflow.com/a/22575530/692942 – user692942 Nov 11 '14 at 12:26
  • 1
    @Lankymart I have included the DateAdd as the preferred solution - thanks. From your referenced answer 'Date formatting options are limited in Classic ASP by default'. I'd say that not supporting all possible format strings remains a limitation. – Pekka Nov 13 '14 at 00:36