|
-
May 27th, 2006, 05:14 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Date difference
Hi everyone!
I've read somewhere a code for getting the difference between two dates...can't remember it, does anyone know how to do?
For example; if you have one date (2006-05-04) and another date (2006-05-09) then the result should be 5.
Now this is an easy example that's very easy to build a code for like:
VB Code:
result = Right(date2, 2) - Right(date1, 2)
But this code that I've seen works even for dates that crosses months like 2006-05-28 and 2006-06-12.
The code I've seen was a built in function in VB...does anyone know what it's called?
Happy for help! 
//Alex
-
May 27th, 2006, 05:34 PM
#2
Re: Date difference
There is a DateDiff() function.
-
May 27th, 2006, 05:37 PM
#3
Re: Date difference
and if you want to calculate the days you can just subtract:
-
May 27th, 2006, 05:42 PM
#4
Fanatic Member
Re: Date difference
Just remember that all dates are just numbers to the computer, and you can get that number by converting to a long, using CLNG()
Don't pay attention to this signature, it's contradictory.
-
May 27th, 2006, 05:47 PM
#5
Re: Date difference
Nooooooooooooooooooooooooo...
Dates are stored as Double so converting it to Long may cause some problem(s), are you kidding ?
-
May 27th, 2006, 05:52 PM
#6
Thread Starter
Addicted Member
Re: Date difference
If you use:
VB Code:
result = "2006-06-10" - Date
you get a mismatch error.
The DateDiff code doesn't either work for me...am I using it wrong?
If I write this:
VB Code:
result = DateDiff(, "2006-06-10", Date)
I get an "Argument not optional" error.
Can anyone help?
-
May 27th, 2006, 05:53 PM
#7
Thread Starter
Addicted Member
-
May 27th, 2006, 05:55 PM
#8
Thread Starter
Addicted Member
Re: Date difference
even this gives a mismatch error:
VB Code:
MsgBox CDbl("2006-06-10") - CDbl(Date)
Plz help me!
-
May 27th, 2006, 06:11 PM
#9
Thread Starter
Addicted Member
Re: Date difference
I've got it!!
The trick is that you have to declare the "future date" before using it:
VB Code:
Dim thedate As Date
thedate = "2006-06-10"
result = DateDiff("d", date, thedate)
Tnx for the tip about DateDiff...I'll reputate at once!
-
May 27th, 2006, 06:26 PM
#10
Re: Date difference
 Originally Posted by cyber_alex
If you use:
VB Code:
result = "2006-06-10" - Date
you get a mismatch error...
That's because first argument is string and second is date - you need to convert firt to date:
result = CDate("2006-06-10") - Date
But I still say DateDiff() is better (safer that is).
-
May 29th, 2006, 10:09 AM
#11
Fanatic Member
Re: Date difference
 Originally Posted by RhinoBull
Nooooooooooooooooooooooooo...
Dates are stored as Double so converting it to Long may cause some problem(s), are you kidding ?
Sorry, I meant you can get the number of days by converting to a long. Usually you don't want the hours, minutes and seconds.
Don't pay attention to this signature, it's contradictory.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|