如何将像20120103这样的数字转换成Excel可以识别的日期? [英] How do I convert a number like 20120103 into a date that Excel can recognize?

查看:130
本文介绍了如何将像20120103这样的数字转换成Excel可以识别的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个8位数字,告诉我日期,YYYYMMDD。我如何将这个数字转换成Excel将会识别为日期的日期。



我们假设单元格A1有20120229 ...我该怎么办? p>

解决方案

既然你标记了这个问题VBA,我假设你想在VBA中得到一个答案,所以在这里你去:

  Dim l As Long 
Dim s As String
Dim d As Date

l = Range( A1)。值'20120229

'将其转换为字符串
s = CStr(l)

'现在可以使用字符串函数来解析
d = DateSerial(CInt(Left(s,4)),CInt(Mid(s,5,2)),CInt(Right(s,2)))
'd现在是2012年02月29日

'将其写回到表格
范围(A2)。值= d


I have an 8 digit number that tells me the date, YYYYMMDD. How can I convert this number into a date that Excel will recognize as a date.

Let's assume cell A1 has 20120229 in it...what do I do?

解决方案

Since you tagged this question VBA, I assume you want an answer in VBA, so here you go:

Dim l As Long
Dim s As String
Dim d As Date

l = Range("A1").Value ' 20120229

' convert it to a string 
s = CStr(l)

' can now use string functions to parse it
d = DateSerial(CInt(Left(s, 4)), CInt(Mid(s, 5, 2)), CInt(Right(s, 2)))
' d is now 29 Feb 2012

' write it back to the sheet
Range("A2").Value = d

这篇关于如何将像20120103这样的数字转换成Excel可以识别的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆