与Convert month name into number一样,我想用DateValue
从日期字符串中创建一个日期。但是,我保证使用美国格式的字符串,而执行代码的PC使用德语区域设置。将美国的日期字符串值转换为本地日期,当前语言环境为<>美国?
例如:DateValue
对于不合格,因为“October”是德语为“Oktober”,所以"10 Okt 2013 06:50:19"
将成功。
I.e.我需要从国家特定日期格式的字符串中获取日期,该日期格式与currert语言环境不同。
如何在不对英文 - 德文月份名称转换表进行硬编码的情况下执行此操作?
此外,如果工作站执行代码重新配置为不同的区域设置,代码应该继续工作,所以我正在寻找一种方法来临时切换设置以通过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
哦,当然!就这么简单。谢谢。 – TheBlastOne
只是[知道哪个螺丝旋转](http://calvinconaway.com/2007/05/27/knowing-which-screw-to-turn/),这就是为什么网站如此伟大! – AutomatedChaos