Solved

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

Posted on 2009-04-09
5
6,419 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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