3

将月份名称转换为数字一样,我想使用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
4

1 回答 1

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
于 2013-10-11T06:57:21.810 回答