VBA Date as integer
Asked Answered
P

5

26

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 ?

For example, for today() I'd like to be able to get back 40877..

Peroxidize answered 30/11, 2011 at 18:16 Comment(0)
W
45

Date is not an Integer in VB(A), it is a Double.

You can get a Date's value by passing it to 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.

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).

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).

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.

Werth answered 30/11, 2011 at 18:22 Comment(6)
Int(CDbl(Now())) does not return a long, it returns a double. The double's value just happens to represent an integer, but it's data type is still double.Sideward
@phoog: TypeName(VarType(Int(CDbl(Now())))) = "Long" on my systemWerth
@Tomalek of course, because VarType(Int(CDbl(Now()))) returns 5, which is the value of vbDouble, but is itself a Long. Try TypeName(Int(CDbl(Now()))).Sideward
If you use Date instead of Now all these part day problems go away - Date (the function not the data type) returns the day with no time component.Baskin
@JonEgerton not to mention that the OP was asking about the Date function in the first place, not the Now function.Sideward
@Werth This was a great answer - to remind us the format is double, not integer - a common misconception and how Int() is like Math.floor (JavaScript). Saved me, too! Thanks.Oldworld
B
17

Just use CLng(Date).

Note that you need to use Long not Integer for this as the value for the current date is > 32767

Baskin answered 30/11, 2011 at 18:20 Comment(5)
Indeed I didnt think of the Long/Integer issue ! Thanks !Peroxidize
CLng will round afternoon values up to the next day.Sideward
@phoog. I've used Date not Now. Date has no time component, therefore CLng is fine and safe.Baskin
@JonEgerton of course. I overlooked that. I didn't cast the downvote, so I can't remove it :( I did add an upvote, however :)Sideward
@phoog: no worries, that makes me net +8!Baskin
G
1
Public SUB test()
    Dim mdate As Date
    mdate = now()
    MsgBox (Round(CDbl(mdate), 0))
End SUB
Gownsman answered 30/11, 2011 at 18:22 Comment(0)
D
1

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

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

I am using this in Office 2007 :)

Devout answered 4/6, 2017 at 8:24 Comment(0)
W
0

This answers the original question of how to get a date portion of a date that doesn't flip to the following day if the time portion is after noon.

Dim JustDate As Date

JustDate = WorksheetFunction.RoundDown(Date_and_Time_Value, 0)

Whereabouts answered 11/1 at 0:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.