2013-10-10 54 views
2

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 

回答

2

您可以使用GetLocaleSetLocale更改地区:

这应该在德语系统上工作,与美国日期

option explicit 

dim currentLocale 
currentLocale = GetLocale() 

SetLocale 1033  ' 1033 is the EN-US locale 
msgbox datevalue("9 Oct 2013 06:50:19") 
' Output: 10/9/2013 

' Put back the original locale 
SetLocale currentLocale 

或周围的其他方式转让;在美国系统上的德国日期

SetLocale 1031  ' 1031 is the German locale 
msgbox datevalue("9 Okt 2013 06:50:19") 
' Output: 09.10.2013 
+0

哦,当然!就这么简单。谢谢。 – TheBlastOne

+0

只是[知道哪个螺丝旋转](http://calvinconaway.com/2007/05/27/knowing-which-screw-to-turn/),这就是为什么网站如此伟大! – AutomatedChaos

相关问题