Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now