excel中的序列号到时间戳

时间:2016-11-10 11:30:46

标签: excel excel-2007

我有 20160715082219000000000 之类的数字,我想转换为 2016-07-15 08:22:19 之类的时间戳,并删除剩余的 0 < /strong>。请在excel中建议如何进行此转换?

2 个答案:

答案 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))

这将输出一个代表日期和时间的数字,因此2016071508221942566.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