WScript number formatting help/question

Leading zeros for intergers?

Hi to all the WScript & VBS gurus - hoping you can give a relative nube a hand...

I'm tring to format numbers in the range 0-99 with a field width of 2 i.e the single digits 0-9 should contain a leading zero makeing 00-09. In other words like the C comand:

printf("%02d",...)

I've been scouring the WScript 5.6 documentation but so far no joy.

My intended purpose is to be able to create a date string in the form YYYYMMDD in which the MM & DD fields must always be 2 digits (and YYYY 4 digits) from the PC's current date value.

Up until now I've been using the FormatDateTime function, but that is dependant on the ShortDate & Locale settings of the PC which has caused me trouble in the past when it changes. It would make life so much easier if I could create this format independantly of the PC Locale by taking the Day, Month & Year values individually, formatting & concatenating them into what I want.

Looking forward to any suggestions...
10,296 views 5 replies
Reply #1 Top
Dare I... ?!   

bump

Reply #2 Top
Somebody ? Anybody ? Bueller ?

Reply #3 Top
(If I have correctly understood you...) Somewhere in your script you have the numbers 0,1,2,3,... but you need see 00,01,02,03,....

Try this simple fynction:

for example your data is named "mynumber"

i.e. mynumber = 0,1,2,3....

desktopx.object("output").text = AddZero(mynumber)

Function AddZero(val)
If len(val) = 1 Then AddZero = "0"&val Else AddZero = val
End Function

Sorry if this couldn't help you.
Reply #4 Top
In VBS you can use FormatDateTime to format according to the computers locale setting. That might be better than to force a custom formatting on people.

Other than that, I've yet not found a native VBS or JScript function that formats numbers, strings like this.

I've ended up making my own padding function.
Reply #5 Top
Thanks Vad_M - your assumption is entirely correct although I was hoping to avoid having to 'roll my own' function so to speak... guess I was spoiled in my C programming days

Thanks also thomassen, your reply is gratefully accepted, although this is what I have used up until now. The problem is my script will be deployed on systems that may have varying configurations for the locale/region settings which is why I was hoping to find a 'universal' solution.

Thanks to both of you for at least clarifying the situation - now I at least know in which direction to proceed!