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.
Ekkehard.Horner
source share