VBScript: PadStr()

14 Feb

This function in VB I wrote to add leading zeros to a stringed number. Useful when converting dates into strings.

For example:

This statement converts date to YYYYMMDD format

StrToday = CStr(Year(Now())) & PadStr(CStr(Month(Now())),2) & PadStr(CStr(Day(Now())),2)

Function PadStr(CStr2Pad, ReqStrLen)

‘Cstr2Pad: stringed number to pad
‘ReqStrLen: final length of the string

PadStr = CStr2Pad

If len(CStr2Pad)

Num2Pad = string(ReqStrlen – len(CStr2Pad)), “0”)
PadStr = Num2Pad & CStr2Pad

End if

End Function

Leave a comment

Posted by on February 14, 2007 in Scripts, VBScripts



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: