Written By TheVBProgramer.
$ReqTestHarness$
The DateDiff function returns a long integer specifying the number of time intervals between two specified dates.
Syntax:
DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
The DateDiff function syntax has these parts:
Part |
Description |
||||||||||||||||||||||
interval |
Required. String expression that is the interval of time you use to calculate the difference between date1 and date2..
The string expression can be any of the following:
|
||||||||||||||||||||||
date1, date2 |
Required; Date. The two dates you want to use in the calculation
|
||||||||||||||||||||||
firstdayofweek |
Optional. A constant 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.
|
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 a date 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.
To demonstrate DateDiff, set up a "Try It" project, and place the following code in the cmdTryIt_Click event:
Private Sub cmdTryIt_Click()
Print "Current date/time is: "; _
Format$(Now, "Long Date"); _
Spc(1); _
Format$(Now, "Long Time")
Print "*** DateDiff Function Examples (1/1/2001 vs. Now) ***"
Print "Using 'yyyy':"; Tab(20); DateDiff("yyyy", #1/1/2001#, Now)
Print "Using 'q':"; Tab(20); DateDiff("q", #1/1/2001#, Now)
Print "Using 'm':"; Tab(20); DateDiff("m", #1/1/2001#, Now)
Print "Using 'y':"; Tab(20); DateDiff("y", #1/1/2001#, Now)
Print "Using 'd':"; Tab(20); DateDiff("d", #1/1/2001#, Now)
Print "Using 'w':"; Tab(20); DateDiff("w", #1/1/2001#, Now)
Print "Using 'ww':"; Tab(20); DateDiff("ww", #1/1/2001#, Now)
Print "Using 'h':"; Tab(20); DateDiff("h", #1/1/2001#, Now)
Print "Using 'n':"; Tab(20); DateDiff("n", #1/1/2001#, Now)
Print "Using 's':"; Tab(20); DateDiff("s", #1/1/2001#, Now)
End Sub
Run the project and click the "Try It" button. The output should look similar to the following:

Download the VB project code for the example above here.
Date query
i design a page like two text boxes..
like one text box contain 1.1.2011..
the second text box contain 3.3.2011..
now my question is HOW TO SELECT data's from ms-Acess database and how to display the data to Data Report....
send me the query..
want Date query
i want a perfect VB6.0 date query for selecting ms_access database detail to data report...
pls send me now.... i search this long days....
I cant calculate the minute and seconds interval of two dates
please help
Text1.Text = "6/1/2011 1:23:34 AM"
Text2.Text = "6/1/2011 12:34:56 AM"
Text3.Text = DateDiff("n", CDate(Text1.Text), CDate(Text2.Text))
how can i get the hours,minutes and seconds?? it always says "type mismatch"
what will i do please help thanks
the answer is a number, but
the answer is a number, but you are trying to put it in a character string variable... hence the type mismatch... EITHER put the answer in a number variable, or convert the number to a string THEN put it in the String variable!
Macro to compare the dates: sys date is compared with two dates
Sub macr_test()
Dim objCurrentDate As Date
objCurrentDate = sysdate
objCurrentDate = Format$(Date, "mm / dd / yyyy")
If ((#7/8/2011# < objCurrentDate) And (objCurrentDate < #7/26/2011#) = True) Then
MsgBox "within the range"
Else
MsgBox "out of range"
End If
-
End Sub
i want a function to
i want a function to calculate date diff. and results should be as
yyyy mm dd
not years only or months only
VB
Problem: I want to calculate the difference between two events(for Eg. event 1 at 10.2.2011at 11:15am & second at 10.3.2011 at 10:15am) in terms of differance of Year ,Month, Day & Hours . Please reply
Datediff
How could I write a code for looking a difference date between "#X#" and "#Y#" if I only need to take on account the weekends of the months (friday, saturday,sunday). In other words, a worker worked since 21-08-2010 until 3-11-2010.His job was just fridays, saturdays and sundays. How many days did the worker work? Thank a lot for your help.
Adolfo from Bucaramanga, Colombia.
datediff()
How should i compute the difference between two dates using two text boxes?
i gave
days=datediff("d",text1.text,text2.text)
please help me
days=datediff("d",text1.text,
days=datediff("d",text1.text,text2.text)
this is ok. just convert the value of both textboxes into date
days=datediff("d",CDate(text1.text),Cdate(text2.text))
Date Format
very Good article, hard to find good articles on vb6. most of the posts resolve into .net related articles.
date manipulation
Really and simply soft
DateDiff problem
About datediff, for example is datediff("YYYY", #12/01/2009#,#01/01/2010#). The two dates have different year and the difference is only 1 month. Is there a way to solve this problem?
try this Print DateDiff("m",
try this
Print DateDiff("m", #12/1/2008#, #1/1/2010#)
btw I still get 1 in the
btw I still get 1 in the result
dim
dim a,b
a=datediff("yyyy",date1,date2)
b=datediff("m",date1,date2)
print a & " year and " & b & " Month"
Difference between two times
To find the difference between two time values use the following formula
For eg to find the no of minutes an employee worked in a day
network=datediff("n",time1,time2)
time1 & time2 should be in time formats like "hh:mm AM/PM"
Similarly we can find hours, seconds etc
good day
hi can u please help me,,,plsss....i used visual basic 6 then how can i set time in and out of employee....let say the time would set in 8am in the morning and out in the 12nn and in the afternoon they would in, in 1pm and out in 5pm.....please help me..........i need ur idea regarding fro this...i hope someone can help me....!tnx
Thanks
Thanks
DateDiff function
thanks... it would be really helpful to all... continue posting...
doubt
dim kk as date
kk =datediff("d",date,"12-nov-2009"
msgbox kk
it displays 12/28/1899
i confusing with this output
thanx
thanx
date add function
how to add a 10 days from current date in vb 6.0
DateDiff or DateDif
what is difference between DateDiff and DateDif
MsgBox CDate("27/11/2009") +
MsgBox CDate("27/11/2009") + 1
no difference between
no difference between datediff and datedif....
the functionalities are same for both .....but datediff is used in vb to calculate interval....and datedif is used in ms excel to calculate the date interval..
Excellent Article
Thanks a lot for this article. This really helped me in saving a lot of time in grabing the system date and time and to work on it.
Please do continue posting.
Alvin James.
http://jamesmathew.uuuq.com
want to format the datediff result to same format as NOW
title says it all. I would like to take the datediff and format it into the same format at the standard NOW output command.
Date Format
The FORMAT function... Something to this effect.
Format(strValue, "MM/dd/yyyy hh:mm:ss")
Learn howto use the Visual Basic DateDiff function
Very Helpfull.
Thanks a lot.
Good article
very Good article, hard to find good articles on vb6. most of the posts resolve into .net related articles.
Post new comment