Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Comparison

Status
Not open for further replies.

kaylabear

Programmer
Oct 23, 2002
59
CA
Hi everyone,

I am a little rusty - haven't written any code in a while, I was hoping somebody would point me in the right direction.

I need a function that will compare today's date(and every subsequent date) with October 19, 2007. I want to know how many days are left from "the current date" until Oct 19 2007.

Can someone throw me a bone please and point me in the right direction?

All your insight is greatly appreciated.
 
DateDiff Function

Returns a Variant (Long) specifying the number of time intervals between two specified dates.

Syntax

DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])

The DateDiff function syntax has thesenamed arguments:

Part Description
interval Required.String expression that is the interval of time you use to calculate the difference between date1 and date2.
date1, date2 Required; Variant (Date). Two dates you want to use in the calculation.
firstdayofweek Optional. Aconstant that specifies the first day of the week. If not specified, Sunday is assumed.
firstweekofyear Optional. A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.


Settings

The intervalargument has these settings:

Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second


The firstdayofweek argument has these settings:

Constant Value Description
vbUseSystem 0 Use the NLS API setting.
vbSunday 1 Sunday (default)
vbMonday 2 Monday
vbTuesday 3 Tuesday
vbWednesday 4 Wednesday
vbThursday 5 Thursday
vbFriday 6 Friday
vbSaturday 7 Saturday

Constant Value Description
vbUseSystem 0 Use the NLS API setting.
vbFirstJan1 1 Start with week in which January 1 occurs (default).
vbFirstFourDays 2 Start with the first week that has at least four days in the new year.
vbFirstFullWeek 3 Start with first full week of the year.


Remarks

You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year.

To calculate the number of days between date1 and date2, you can use either Day of year ("y") or Day ("d"). When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it doesn't count date1, even if it does fall on a Sunday.

If date1 refers to a later point in time than date2, the DateDiff function returns a negative number.

The firstdayofweek argument affects calculations that use the "w" and "ww" interval symbols.

If date1 or date2 is adate literal, the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in double quotation marks (" "), and you omit the year, the current year is inserted in your code each time the date1 or date2 expression is evaluated. This makes it possible to write code that can be used in different years.

When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thanks. All I did was look it up in the MSDN and cut-n-paste it here.

If you don't have MSDN installed, you should. It make take some searching and reading sometimes, but there's a lot of good info there.

You can also get it online at

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
kaylabear - Coincidently, how many days is it until my 26th birthday?

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
To get the difference of two dates, you can also subtract the two dates numerically.
___
[tt]
Dim D As Date, nDays As Integer
D = "Oct 19 2007" '= #10/19/2007#
nDays = D - Date
MsgBox nDays[/tt]
 
Hypetia -
I'm not sure that's a good plan -- it doesn't take into account things like leap years, and is making use of the fact that dates are stored internally as doubles (ie. using a side-effect).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
>it doesn't take into account things like leap years

Well, it does.
___
[tt]
Dim D1 As Date, D2 As Date, D3 As Date
D1 = #1/1/2003#
D2 = #1/1/2004# 'leap year
D3 = #1/1/2005#
Debug.Print D3 - D2, D2 - D1[/tt]
___

As far as floating point issues are concerned, VB's DateDiff function also handles them in the same way (probably). Since nDays is an Integer, VB automatically rounds the result to an integer value, fixing any floating point errors if they occur. If you store the return value in a floating point number, VB will return the exact difference between the two dates, which may be a fraction, like 5.5 days.

I also used to use the DateDiff function in the past, but now I prefer this date arithmatic and use it whenever applicable.

This method is very fast as compared to DateDiff function. It is also free from the "interval difference" issue associated with DateDiff function, as explained by strongm and others many times in past threads.

The DateDiff function does not return the exact numerical difference between the two dates. But the number of specified intervals elapsed between the two dates.

[tt]MsgBox DateDiff("d", #7/6/2005 11:59:59 PM#, #7/7/2005 12:00:01 AM#)[/tt]

The above code returns a difference of 1 day, although the actual difference between the two dates is only 2 seconds. This is not a bug, but the behaviour of this function. It might be useful in some cases, but most of the time it is confusing.

Lastly, I would say that using DateDiff is not a wrong practice. This is just my personal preference. I do not want to stop anybody from using DateDiff function.:)
 
That would be the way to do it if I wanted to know how many seconds apart two date/time values are. But if you want to know is how far apart two date values are, why not take the easy way and get precisely the answer you want? You can also find the difference in other intervals without doing any manual conversion.
the actual difference between the two dates is only two seconds
Incorrect. The actual difference between the two times may be two seconds, but the difference between the two dates is one day.
This is not a bug, but the behaviour of this function
Of course it's not a bug, you ASKED for the difference in DAYS.
It might be useful in some cases, but most of the time it is confusing.
Why should it be confusing for you to get exactly what you asked for? If you wanted to know that the two date/times are only two seconds apart, you should have asked for the difference in seconds, not days.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top