我有 20160715082219000000000 之类的数字,我想转换为 2016-07-15 08:22:19 之类的时间戳,并删除剩余的 0 < /strong>。请在excel中建议如何进行此转换?
答案 0 :(得分:1)
对单元格 A1
中的数字使用以下公式=DATE(LEFT(A1*10^-9&"",4),MID(A1*10^-9&"",5,2),MID(A1*10^-9&"",7,2))+TIME(MID(A1*10^-9&"",9,2),MID(A1*10^-9&"",11,2),MID(A1*10^-9&"",13,2))
这将输出一个代表日期和时间的数字,因此20160715082219
由42566.34883
表示。将自定义格式应用于此单元格(右键单击&gt;格式化单元格&gt;自定义)并在Type:
字段中键入以下字符串
yyyy-mm-dd hh:mm:ss
你会得到结果:
2016-07-15 08:22:19
根据需要
答案 1 :(得分:-1)
这是一个对我有用的解决方案。我知道会有一个优化的解决方案,少量代码和字符串操作。欢迎提出建议。
Dim Val As String
Dim StrL As String
Dim FinalVal As String
'Store the value in a string
Val = "20160715082219000000000"
'Pick first 4 digits: yyyy
StrL = Left(Val, 4)
'Store the year value in a final string
FinalVal = StrL & "-"
'Trim first 4 digits
Val = Right(Val, (Len(Val) - 4))
'Pick first 2 digits from the trimmed string val: mm
StrL = Left(Val, 2)
'Store the month value in a final string
FinalVal = FinalVal & StrL & "-"
'Trim first 2 digits
Val = Right(Val, (Len(Val) - 2))
'Pick first 2 digits from the trimmed string val: dd
StrL = Left(Val, 2)
'Store the day value in a final string
FinalVal = FinalVal & StrL & " "
'Trim first 2 digits
Val = Right(Val, (Len(Val) - 2))
'Pick first 2 digits from the trimmed string val: HH
StrL = Left(Val, 2)
'Store the hour value in a final string
FinalVal = FinalVal & StrL & ":"
'Trim first 2 digits
Val = Right(Val, (Len(Val) - 2))
'Pick first 2 digits from the trimmed string val: MM
StrL = Left(Val, 2)
'Store the mins value in a final string
FinalVal = FinalVal & StrL & ":"
'Trim first 2 digits
Val = Right(Val, (Len(Val) - 2))
'Pick first 2 digits from the trimmed string val: ss
StrL = Left(Val, 2)
'Store the month value in a final string
FinalVal = FinalVal & StrL
'Trim first 2 digits
Val = Right(Val, (Len(Val) - 2))
'Discard remaining characters
Val = ""
StrL = ""
MsgBox FinalVal