在Excel中转​​换TZ数据

时间:2013-08-28 11:20:54

标签: vba

我正在使用Excel从日志文件中显示时间戳。文件原始输出中的时间戳以毫秒精度显示,我需要维护。时间戳也是UTC,我需要转换为我当地的时区。当我使用dateAdd函数转换为我的TZ偏移时,它将毫秒值设置为'000'。

GetLocalTimeFromGMT = DateAdd("h", offset, dateToConvert)

有没有办法在不丢失精度的情况下使用dateAdd,还是有另一种更适合该任务的函数?

2 个答案:

答案 0 :(得分:0)

请查看以下链接了解更多详情。

http://www.cpearson.com/excel/TimeZoneAndDaylightTime.aspx

Function GetLocalTimeFromGMT(Optional GMTTime As Date) As Date
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' GetLocalTimeFromGMT
    ' This returns the Local Time from a GMT time. If GMTTime is present and
    ' greater than 0, it is assumed to be the GMT from which we will calculate
    ' Local Time. If GMTTime is 0 or omitted, it is assumed to be the GMT
    ' time.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim GMT As Date
    Dim TZI As TIME_ZONE_INFORMATION
    Dim DST As TIME_ZONE
    Dim LocalTime As Date

    If StartTime <= 0 Then
        GMT = Now
    Else
        GMT = GMTTime
    End If
    DST = GetTimeZoneInformation(TZI)
    LocalTime = GMT - TimeSerial(0, TZI.Bias, 0) + IIf(DST = TIME_ZONE_DAYLIGHT, TimeSerial(1, 0, 0), 0)
    GetLocalTimeFromGMT = LocalTime

    End Function

答案 1 :(得分:0)

我的第一个想法是使用格式(YourDate,“mm / dd / yyyy hh:mm:ss.000”),但它不起作用。我认为DateAdd肯定是罪魁祸首,直到我试图将约5小时添加到约会。在工作表中完成后,保留毫秒数。在VBA中完成时,不会保留毫秒数。这可能与Excel如何处理时间和日期有关。 DateAdd以及VBA的范围都没有设置为执行此操作。

最快捷,最简单的方法有点脏,但它有效。将所有时间放入工作表中,然后使用

添加5个小时
    ="CellLocationOfGMTDate" + (offset/24)

这将为您提供所需的时间并保留毫秒数。然后,您可以从工作表中提取时间和日期。

如果你真的需要在VBA中这样做,你必须弄清楚如何从你的日期拉出毫秒,然后在添加偏移后再添加它们。如果我找到了什么,我会试着弄明白并稍后报告。