Solved

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

Posted on 2009-04-09
5
6,673 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:xbanditox
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24108308
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

0
 

Author Comment

by:xbanditox
ID: 24108577
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!
0
 

Author Closing Comment

by:xbanditox
ID: 31568563
Perfect solution
0
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 25900272
Nice Code :)

thanks Idle_Mind
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 25900348
=)
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question