Date format in VBS

I want to get the date in full format in vbscript . For example, I set DateParam = FormatDateTime(Date()-1, 2)

But he returns

3/8/2012
I need to function for a return 03/08/2012 .

Does anyone know how to fix this?

0
vbscript
source share
2 answers

The FormatDateTime function is useless because it depends on user and global regional settings.

The best solution (at most for least effort) - clicking on .NET is wrong for dates; again due to regional settings.

If you need / need to collapse your own function, start with something like fmtDate ().

 Dim g_oSB : Set g_oSB = CreateObject("System.Text.StringBuilder") Function sprintf(sFmt, aData) g_oSB.AppendFormat_4 sFmt, (aData) sprintf = g_oSB.ToString() g_oSB.Length = 0 End Function Function fmtDate(dtX) fmtDate = Join(Array( _ Right(100 + Month(dtX), 2) _ , Right(100 + Day(dtX), 2) _ , Year(dtX) _ ), "/") End Function Dim dtYesterday : dtYesterday = Date() - 1 WScript.Echo "Yesterday:", dtYesterday, GetLocale() WScript.Echo "sprintf (silly) =>", sprintf("{0:MM/dd/yyyy}", Array(dtYesterday)) WScript.Echo "sprintf (clumsy) =>", sprintf("{0:MM}/{0:dd}/{0:yyyy}", Array(dtYesterday)) WScript.Echo "fmtDate =>", fmtDate(dtYesterday) 

exit:

 Yesterday: 08.03.2012 1033 sprintf (silly) => 03.08.2012 sprintf (clumsy) => 03/08/2012 fmtDate => 03/08/2012 

With another thought:

Exiting "/" helps make sprintf () applicable:

 WScript.Echo "sprintf (silly me) =>", sprintf("{0:MM\/dd\/yyyy}", Array(dtYesterday)) 

exit:

 sprintf (silly me) => 03/08/2012 

So don't worry about fmt * functions, but use .NET formatting.

+1
source share
 ThisDate = Date() MyDate = Right("0" & CStr(Month(ThisDate)), 2) & "/" & _ Right("0" & CStr(Day(ThisDate)), 2) & "/" & CStr(Year(ThisDate)) 
-2
source share

All Articles