vba 将日期转换为日期序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6583312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:36:17  来源:igfitidea点击:

convert date to date serial

excelvba

提问by michael.t

I'm trying to convert a date (date type) into a long. This long should be something like the number of days since the 1 January 1900. How to get this in VBA ? In excel I'm getting this automatically when i concatenate a date with a string.

我正在尝试将日期(日期类型)转换为 long。这个长应该是自 1900 年 1 月 1 日以来的天数。如何在 VBA 中得到这个?在 excel 中,当我将日期与字符串连接时,我会自动获取此信息。

回答by Kevin Bowersox

Function dateToLong(ByVal d As Date) As Long

dateToLong = CLng(d)
End Function 

If you need to capture the time in the long use this:

如果您需要长时间捕获时间,请使用以下命令:

Function Dt2Lng(aDate As Date) As Long
   '-- acept datetime range from MinDate to
   '-- (MinDate + 49710 days + 6:28:15) ~ 136 years
   Dt2Lng = CLng((aDate - MinDate) * 86400 - 2 ^ 31)
End Function

回答by bvukas

Say if you have "1/1/2016" in A1, you could use Range("A1").Value2 to get the date serial number.

假设您在 A1 中有“1/1/2016”,您可以使用 Range("A1").Value2 来获取日期序列号。