date format in VBS
我想在vbscript中获取完整格式的日期。 例如,我把
但它返回
我需要返回功能
有谁知道如何解决这个问题?
FormatDateTime函数是无用的,因为它取决于特定于用户的全局区域设置。
最好的(省力省力)解决方案-进入.NET-日期有缺陷。 再次由于依赖于区域设置。
如果要/需要滚动自己的函数,请从类似fmtDate()的内容开始。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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) |
输出:
1 2 3 4 | Yesterday: 08.03.2012 1033 sprintf (silly) => 03.08.2012 sprintf (clumsy) => 03/08/2012 fmtDate => 03/08/2012 |
再三考虑:
转义" /"有助于使sprintf()可用:
1 | WScript.Echo"sprintf (silly me) =>", sprintf("{0:MM\/dd\/yyyy}", Array(dtYesterday)) |
输出:
1 | sprintf (silly me) => 03/08/2012 |
因此,不要理会fmt *函数,而要使用.NET格式。
1 2 3 4 | ThisDate = Date() MyDate = Right("0" & CStr(Month(ThisDate)), 2) &"/" & _ Right("0" & CStr(Day(ThisDate)), 2) &"/" & CStr(Year(ThisDate)) |