![](/img/trans.png)
[英]I want to change UTC date time stamp to IST date time stamp in online excel
[英]Convert UTC time stamp in text to Excel date
我在 Excel 工作簿中有日期,格式为
2019 年 2 月 7 日星期四 09:38:41 UTC+10
它们的格式为一般/文本。 需要转换为实际的 Excel 日期/时间进行排序。
尝试解析和拆分但并不总是有效并且非常笨重
解析似乎是唯一明智的方法。 不过看起来像是正则表达式的工作。
该函数需要引用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
另外,出于兴趣,如果您有带有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
我没有任何“笨拙”的 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)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.