【问题标题】:Convert UTC time stamp in text to Excel date将文本中的 UTC 时间戳转换为 Excel 日期
【发布时间】:2019-10-29 09:01:27
【问题描述】:

我在 Excel 工作簿中有日期,格式为

2 月 7 日星期四 09:38:41 UTC+10 2019

它们被格式化为一般/文本。需要转换成实际的 Excel 日期/时间进行排序。

尝试过解析和拆分,但并不总是有效,而且非常笨重

【问题讨论】:

  • 你笨拙的失败解析代码是什么样的?
  • 什么版本的 Excel?

标签: excel vba date time


【解决方案1】:

解析似乎是唯一明智的方法。不过看起来像是正则表达式的工作。

此函数需要引用VBScript_RegEp_55 类型库:

Public Function ParseUtcDate(ByVal value As String, Optional ByVal utcOffset As Double = 0) As Date
    Const pattern As String = "(\w+) (\w+) (\d+) (\d\d:\d\d:\d\d) UTC((\+|\-)\d+) (\d\d\d\d)"
    With New RegExp
        .IgnoreCase = True
        .Global = True
        .pattern = pattern

        Dim mc As MatchCollection
        Set mc = .Execute(value)
    End With

    Dim m As Match
    Set m = mc(0)

    Dim monthNamePart As String
    monthNamePart = m.SubMatches(1)

    Dim dayOfMonthPart As String
    dayOfMonthPart = m.SubMatches(2)

    Dim timePart As String
    timePart = m.SubMatches(3)

    Dim utcOffsetPart As String
    utcOffsetPart = m.SubMatches(4)

    Dim yearPart As String
    yearPart = m.SubMatches(6)

    Dim dateParts As Variant
    dateParts = VBA.Array(monthNamePart, dayOfMonthPart, yearPart, timePart)

    Dim formattedDate As String
    formattedDate = VBA.Join(dateParts, " ")

    Dim offset As Double
    offset = CDbl(utcOffsetPart)

    Dim offsetHours As Double
    offsetHours = offset / 24

    Dim targetOffset As Double
    targetOffset = utcOffset / 24

    ParseUtcDate = CDate(formattedDate) - offsetHours + targetOffset

End Function

用法:

?ParseUtcDate("Thu Feb 7 09:38:41 UTC+10 2019", 10)
 2/7/2019 9:38:41 AM

?ParseUtcDate("Thu Feb 7 09:38:41 UTC+10 2019")
 2/6/2019 11:38:41 PM

?ParseUtcDate("Thu Feb 7 09:38:41 UTC+10 2019", -5)
 2/6/2019 6:38:41 PM

【讨论】:

    【解决方案2】:

    另外,出于兴趣,如果您有带有 TEXTJOIN 函数的 Excel 2016+,您可以使用 FILTERXML 解析段,然后创建 Excel 将解释为真实日期的日期/时间字符串:

    当地时间

    =TEXTJOIN(" ",TRUE,INDEX(FILTERXML("<t><s>" & SUBSTITUTE(A2," ","</s><s>")& "</s></t>","//s"),N(IF(1,{3,2,6}))))
    +FILTERXML("<t><s>" & SUBSTITUTE(A2," ","</s><s>")& "</s></t>","//s[4]")
    

    UTC 时间

    =TEXTJOIN(" ",TRUE,INDEX(FILTERXML("<t><s>" & SUBSTITUTE(A2," ","</s><s>")& "</s></t>","//s"),N(IF(1,{3,2,6}))))
    +FILTERXML("<t><s>" & SUBSTITUTE(A2," ","</s><s>")& "</s></t>","//s[4]")
    -SUBSTITUTE(FILTERXML("<t><s>" & SUBSTITUTE(A2," ","</s><s>")& "</s></t>","//s[5]"),"UTC","")/24
    

    【讨论】:

      【解决方案3】:

      我没有任何“笨拙”的 VBA 代码,但我已经制定了工作表公式。

      'for localized date/time
      =SUM(DATEVALUE(TRIM(MID(REPLACE(A2, FIND(" ", A2, 9), 16, ", "), 4, LEN(A2)))),
           TIMEVALUE(MID(A2, FIND(" ", A2, 9)+1, 8)))
      'for UTC date time
      =SUM(DATEVALUE(TRIM(MID(REPLACE(A2, FIND(" ", A2, 9), 16, ", "), 4, LEN(A2)))),
           TIMEVALUE(MID(A2, FIND(" ", A2, 9)+1, 8)),
           -PRODUCT(VALUE(MID(A2, FIND("UTC", A2)+3, 3)), TIME(1, 0, 0)))
      

      【讨论】:

      • 欢迎来到 SO!令人印象深刻的工作!不过老实说,我发现公式的可读性/可维护性相当成问题。这是 UDF 符合 IMO 要求的情况之一。也就是说,我做出了自己的回答“社区维基”,因为就站点规则而言,OP 的问题是偏离主题的(OP 没有明显努力解决手头的问题)..但像你一样(我猜),我认为这是一个有趣的问题需要解决,所以我还是发布了 - 但看到这个问题最终被关闭不要感到惊讶。去回答一些热点问题!
      • 对不起大家,发生了一场严重的车祸,一直没有时间添加更多细节。
      猜你喜欢
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 2021-06-14
      • 2018-07-23
      相关资源
      最近更新 更多