与将月份名称转换为数字一样,我想使用DateValue
从日期字符串创建日期。但是,我所拥有的字符串保证是美国格式,而执行代码的 PC 是在德国语言环境下运行的。
示例:DateValue
失败 for "10 Oct 2013 06:50:19"
,它将成功 for"10 Okt 2013 06:50:19"
因为“October”在德语中是“Oktober”。
即,我需要从与当前语言环境不同的国家特定日期格式的字符串中获取日期。
如果不对英语到德语的月份名称翻译表进行硬编码,我该如何做到这一点?
此外,如果执行代码的工作站被重新配置为不同的语言环境,代码应该继续工作,所以我正在寻找一种方法来临时切换设置以通过 获取美国日期DateValue
,然后切换回原始设置。当然,Windows 风格中的代码应该是可移植的......
当前存在此问题的代码如下所示。它试图通过查看 http 标头来获取特定服务器的系统时间。如果英语中的当前(短)月份名称在德语中不同,则会失败。
Public Function GetServerTimeFromUrl (ByVal Url,ByRef DT)
Dim HttpRequest: Set HttpRequest = CreateObject("Microsoft.XMLHttp")
HttpRequest.Open "GET" , Url, false
HttpRequest.Send
Dim Result: Result=(HttpRequest.Status = 200)
If Result then
Dim DateS: DateS=HttpRequest.getResponseHeader ("Date")
DateS=Right (DateS,Len (DateS)-InStr (DateS,",")-1)
' DateS has weekday removed
DateS=Left (DateS,InStrRev (DateS," ")-1)
' DateS has timezone removed
' DateS now holds something like "09 Sep 2013 11:49:54"
Dim ServerTimeGMT: ServerTimeGMT=DateValue (DateS)+TimeValue (DateS)
Dim Delta: Delta=CreateObject("WScript.Shell").RegRead("HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
' Delta now holds "skew" value for time zone including DST
DT=DateAdd("n", -Delta,ServerTimeGMT)
Result=true
End If
GetServerTimeFromUrl=Result
End Function