Friday, August 11, 2006

VBScript: Add Leading Zero to Date Function Output

When you generate reports/files using VBScript periodically, it is always a good idea to suffix the file name with date/time components - e.g., "REPORT_20060811" - as it makes files easier to locate because they would already be sorted.

Using functions Year( Date ), Month( Date ), Day( Date ), subcomponents can be extracted to create filename suffix. You can also use DatePart( datetype,date ) function to extract the same components. But the problem with using any of these functions as they are, is that you would not get proper sorting orders because these will output single digits for numbers less than 10. For example, the following two lines of code will generate date components which will not have proper sorting order

strDate = Year(Date) & Month(Date) & Day(Date)
OR
strDate = DatePart("yyyy",Date) _
        & DatePart("m",Date) _
        & DatePart("d",Date)

To generate the string which will have the right sorting order, you need to append leading zeros to entries less than 10. That is what the following line of code does. It appends leading zero to all the entities, extracts 2 characters from right, and builds the string.

strDate = DatePart("yyyy",Date) _
        &
Right("0" & DatePart("m",Date), 2) _
        &
Right("0" & DatePart("d",Date), 2)

21 Comments:

Anonymous Anonymous said...

Elegant! Only other function I found to do the exact same thing was about 30 lines of code. Thanks.

Wednesday, December 03, 2008 5:58:00 AM  
Anonymous Anonymous said...

Wow, this is awesomely simple and just what I needed in my script. Thanks!

Friday, December 05, 2008 1:24:00 AM  
Anonymous DP in SF said...

Absolutely beautiful. I needed the same thing. Either the dates are wrong on the two comments above, or for some nutty reason the last two days have generated a number of people needing and appreciating your post over 2 years ago!

Friday, December 05, 2008 3:24:00 AM  
Anonymous T. Nickey said...

Thank you so much. I looked high and low for example code on how to save a date in reverse and this works perfect.

Wednesday, January 14, 2009 12:19:00 AM  
Anonymous Anonymous said...

Thanks, I used this to get leading zeros in a timestamp as well:

strTime = Right("0" & Hour(Now), 2) & ":" & Right("0" & Minute(Now), 2) & ":" & Right("0" & Second(Now), 2)

Tuesday, January 27, 2009 4:13:00 AM  
Blogger mbelow said...

This comment has been removed by the author.

Thursday, January 29, 2009 11:30:00 PM  
Anonymous Anonymous said...

I have to go back through years of code and remove my clunky AddLeadingZero() functions. Thanks for posting this!

Saturday, April 11, 2009 3:03:00 AM  
Blogger paolo said...

!bravis! this is great

Tuesday, May 12, 2009 10:19:00 PM  
Anonymous Anonymous said...

Amazingly simple yet perfect for what I need.

Tuesday, July 07, 2009 7:36:00 AM  
Blogger Grozny said...

strDate = Format(Date, "yyyymmdd")

Will do the same. Looks shorter to me.

See also
http://msdn.microsoft.com/en-us/library/73ctwf33(VS.80).aspx

Saturday, July 25, 2009 7:56:00 AM  
Blogger Raj said...

Format function doesn't work in VBScript, works only in Visual Basic.
http://www.4guysfromrolla.com/webtech/051601-1.shtml

Saturday, July 25, 2009 6:52:00 PM  
Anonymous irontomsk said...

Very handy! Thank you very much.

Monday, March 29, 2010 4:42:00 PM  
Anonymous Anonymous said...

Thanks!!!

Thursday, January 06, 2011 7:01:00 AM  
Anonymous Anonymous said...

Cheers, Just wat was needed.

Thursday, January 27, 2011 7:45:00 AM  
Blogger DermottB said...

Wow, beautiful code, ver nice!

Wednesday, July 20, 2011 7:24:00 PM  
Anonymous drofmij said...

Excellent!

I just used this code snippet to cleanup a batch file that used dated file names. Very helpful and much cleaner.

~drofmij

Monday, September 12, 2011 11:50:00 PM  
Anonymous Anonymous said...

Great! Thank you so much!

Wednesday, January 11, 2012 8:29:00 AM  
Blogger Theo Macris said...

Just what I needed. Thanks!

Wednesday, May 02, 2012 1:57:00 PM  
Anonymous Anonymous said...

Perfect! Just what I needed for my RoboCopy script.

Friday, May 11, 2012 11:14:00 PM  
Anonymous Anonymous said...

very clever : )

Tuesday, August 07, 2012 12:03:00 AM  
Anonymous Anonymous said...

Nicely done! I've been looking all over the place for a short elegant solution... this is the only one!

Thanks!

Friday, October 05, 2012 11:21:00 PM  

Post a Comment

Links to this post:

Create a Link

<< Home