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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you calculate the difference between two dates? 1

Status
Not open for further replies.

panini

MIS
Jun 1, 2001
136
GB
For example to establish someones age from their date of birth?

Thanks,
 
Here is some examples...
Code:
His sintax is
DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])

Arguments
interval 
Required. String expression that is the interval you want to use to calculate the differences between date1 and date2. See Settings section for values. 
date1, date2 
Required. Date expressions. Two dates you want to use in the calculation. 
firstdayofweek 
Optional. Constant that specifies the day of the week. If not specified, Sunday is assumed. See Settings section for values. 
firstweekofyear 
Optional. 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. See Settings section for values. 

--------------------
DiffADate = "Days from today: " & DateDiff("d", Now, theDate)
Where the "d" could be some of the following:
Setting Description 
yyyy       Year 
q         Quarter 
m          Month 
y        Day of year 
d           Day 
w         Weekday 
ww      Week of year 
h          Hour 
n         Minute 
s         Second 

The firstdayofweek argument can have the following values:

Constant             Value     Description 
vbUseSystemDayOfWeek   0      Use National Language Support (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 

The firstweekofyear argument can have the following values:

Constant Value Description 
vbUseSystem 0 Use National Language Support (NLS) API setting. 
vbFirstJan1 1 Start with the week in which January 1 occurs (default). 
vbFirstFourDays 2 Start with the week that has at least four days in the new year. 
vbFirstFullWeek 3 Start with the first full week of the new 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 a date literal, the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in 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.

Hope this helps... ________
George, M
 
George, thanks for all your help - is datediff aware of leap years?
 
By using the DateDiff and based on the birthday submitted, this will return the amount of days that have passed from the birthday to the current date, as well as the months and the years. I have an .htm page with the input here's the code

<%@ LANGUAGE=&quot;VBSCRIPT&quot; %>
<% Option Explicit %>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<B>Please enter your name and birthdate:</B><BR>

<FORM METHOD=POST ACTION=&quot;YourAge.asp&quot;>
Your Name: <INPUT TYPE=TEXT NAME=Name>
<BR>

Your Birthdate (in MM/DD/YY format):<BR>
<INPUT TYPE=TEXT NAME=Birthdate>

<P>
<INPUT TYPE=SUBMIT VALUE=Send!>
</FORM>
</body>
</html>

then it submits to an .asp page for processing here's that page.

<%@ LANGUAGE=&quot;VBSCRIPT&quot; %>
<% Option Explicit %>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<%
'Read in the form field variables
Dim strName, dtBirthDate
strName = Request.Form(&quot;Name&quot;)
dtBirthDate = Request.Form(&quot;Birthdate&quot;)

Dim iDaysOld
iDaysOld = DateDiff(&quot;d&quot;, dtBirthDate, Date)

Dim iMonthsOld
iMonthsOld = Int(iDaysOld / 30)

Dim iYearsOld
iYearsOld = Int(iMonthsOld / 12)
%>
Hello <%=strName%>!
You were born on <%=dtBirthDate%>.
<BR>
That makes you <%=iYearsOld%> years old.
<BR>
by the way, that's <%=iMonthsOld%> months,
<BR>
and <%=iDaysOld%> days.
</body>
</html>

now if i'm correct in saying this, the iDaysOld uses the DateDiff to get the difference in Days &quot;d&quot; from the birthday entered and the current date. perhaps there are some flaws in the code, but it does work. hope this helps.. :)
 
I have a similar question. I am trying to use the DateDiff function to determine how many minutes occur between two dates. I am using:


DateDiff(&quot;m&quot;,[TimeLock],Now())


...where [TimeLock] is a field from my table. The dates and times it is pulling are:

Now(): 7/15/2002 2:53:30 PM
TimeLock: 7/15/2002 2:37:52 PM

However, it is telling me that there are no minutes between these two dates? Can someone tell me what I'm doing wrong? Thanks!
 
Oh, I see what I did wrong. I used a &quot;m&quot; for minute, when it's really a &quot;n&quot;. Nevermind.. thanks though!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top