Solved

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

Posted on 2009-04-09
5
7,432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

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

1
 

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 86

Expert Comment

by:Mike Tomlinson
ID: 25900348
=)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

628 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