excel 中时间戳的序列号

Serial number to timestamp in excel

我有一个像 20160715082219000000000 这样的数字,我想转换成像 2016-07-15 08:22:19 这样的时间戳,并且删除剩余的 0's.Please 建议如何在 excel?

中进行此转换

这是一个对我有用的解决方案。我知道会有一个优化的解决方案,代码量和字符串操作更少。欢迎提出建议。

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

对单元格 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 表示。将自定义格式应用于此单元格(右键单击>格式化单元格>自定义)并在 Type: 字段中键入以下字符串

yyyy-mm-dd hh:mm:ss

你会得到结果:

2016-07-15 08:22:19

根据需要