VBA 日期为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8330838/
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
VBA Date as integer
提问by BuZz
is there a way to get the underlying integer for Date
function in VBA ? I'm referring to the integer stored by Excel to describe dates in memory in terms of number of days (when time is included it can be a float then I guess). I'm only interest in the integer part though. Is there just another function for that ?
有没有办法Date
在 VBA 中获取函数的底层整数?我指的是 Excel 存储的整数,以根据天数来描述内存中的日期(当包含时间时,我猜它可以是浮点数)。不过我只对整数部分感兴趣。是否只有另一个功能?
For example, for today() I'd like to be able to get back 40877..
例如,对于 today() 我希望能够取回 40877..
Thank you guys ;)
谢谢你们 ;)
回答by Tomalak
Date is not an Integer in VB(A), it is a Double.
日期在 VB(A) 中不是整数,而是双精度数。
You can get a Date's value by passing it to CDbl()
.
您可以通过将日期的值传递给CDbl()
.
CDbl(Now()) ' 40877.8052662037
From the documentation:
从文档:
The 1900 Date System
In the 1900 date system, the first day that is supported is January 1, 1900. When you enter a date, the date is converted into a serial number that represents the number of elapsed days starting with 1 for January 1, 1900. For example, if you enter July 5, 1998, Excel converts the date to the serial number 35981.
1900 日期系统
在 1900 日期系统中,支持的第一天是 1900 年 1 月 1 日。当您输入日期时,该日期将转换为一个序列号,该序列号表示 1900 年 1 月 1 日从 1 开始所经过的天数。例如, 如果您输入 1998 年 7 月 5 日,Excel 会将日期转换为序列号 35981。
So in the 1900 system, 40877.805...
represents 40,876 days after January 1, 1900 (29 November 2011), and ~80.5% of one day (~19:19h). There is a setting for 1904-based system in Excel, numbers will be off when this is in use (that's a per-workbook setting).
因此,在 1900 系统中,40877.805...
代表1900 年1 月 1 日(2011 年 11 月 29 日)之后的 40,876 天,以及一天的约 80.5%(约 19:19h)。Excel 中有一个基于 1904 系统的设置,使用时数字将关闭(这是每个工作簿的设置)。
To get the integer part, use
要获取整数部分,请使用
Int(CDbl(Now())) ' 40877
which would return a LongDouble with no decimal places (i.e. what Floor()
would do in other languages).
这将返回一个没有小数位的LongDouble(即Floor()
在其他语言中会做什么)。
Using CLng()
or Round()
would result in rounding, which will return a "day in the future" when called after 12:00 noon, so don't do that.
使用CLng()
orRound()
会导致四舍五入,在中午 12:00 之后调用时将返回“未来的一天”,所以不要这样做。
回答by Jon Egerton
Just use CLng(Date)
.
只需使用CLng(Date)
.
Note that you need to use Long
not Integer
for this as the value for the current date is > 32767
请注意,您需要使用Long
notInteger
作为当前日期的值 > 32767
回答by xQbert
Public SUB test()
Dim mdate As Date
mdate = now()
MsgBox (Round(CDbl(mdate), 0))
End SUB
回答by IshaniNet
You can use bellow code example for date string like mdate and Now() like toDay, you can also calculate deference between both date like Aging
您可以将以下代码示例用于日期字符串,如 mdate 和 Now(),如 toDay,您还可以计算两个日期之间的差异,如 Aging
Public Sub test(mdate As String)
Dim toDay As String
mdate = Round(CDbl(CDate(mdate)), 0)
toDay = Round(CDbl(Now()), 0)
Dim Aging as String
Aging = toDay - mdate
MsgBox ("So aging is -" & Aging & vbCr & "from the date - " & _
Format(mdate, "dd-mm-yyyy")) & " to " & Format(toDay, "dd-mm-yyyy"))
End Sub
NB: Used CDate
for convert Date String to Valid Date
注意:用于CDate
将日期字符串转换为有效日期
I am using this in Office 2007 :)
我在 Office 2007 中使用它 :)