• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8746
  • Last Modified:

VBScript: Get previous day's date in YYMMDD format?

Hi, I am trying to get a particular piece of VBScript to give me the previous days date in YYMMDD format.

Here is some sample code I found online and modified for my needs, however, this code does not work on the 1st day of each month (IE: If it is April 1, 2009 the script returns 090431 instead of 090331).

I have tried other ways to get the date in the proper format as well, the code below works fine to get the current date in YYMMDD format.

I also tried the second snippet below, however this time I am losing the leading zero if the DAY, or MONTH are single digit (IE: if it is April 1, 2009 the script returns 0941 instead of 090401).

It is imperative the script does not drop the leading zeroes from the days or months, as I am using it to retrieve IIS log files...  It feels like I almost have this figured out, but I can't quite come to a solution. Please help!
'Code snippet #1'
Dim yesterday
yesterday = Right(Year(Date),2) & Right("0" & Month(Date),2) & Right("0" & Day(Date() -1),2)
Wscript.echo yesterday
'Code snippet #2'
logDate = FormatDateTime(date() -2,2)
logDateP = Replace(logDate, "/", "")
wscript.Echo logDateP

Open in new window

  • 2
  • 2
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try this out:

Dim dt, yesterday
dt = DateAdd("d", -1, Date)
yesterday = Right(Year(dt),2) & Right("0" & Month(dt),2) & Right("0" & Day(dt),2)

Open in new window

xbanditoxAuthor Commented:
That works perfectly.  I tested it on various days by change my system clock and it gave me an accurate date in the correct format every time...

Thanks so much!
xbanditoxAuthor Commented:
Perfect solution
Kamaraj SubramanianApplication Support AnalystCommented:
Nice Code :)

thanks Idle_Mind
Mike TomlinsonMiddle School Assistant TeacherCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now