The Daily Newbie - Using the DateDiff() Function
Newest in the series aimed at teaching newbies (and not so newbies) about the commands that VB has available. This edition gives a basic outline of DateDiff() usage and, as always, some copt-and-paste sample code.
Original Author: Matt Roberts
Code
The “To Start Things Third About this Today's Newbie code is the result of a request from a reader of yesterdays (thanks for the suggestion BigCalm). Today I am going to discuss the DateDiff() function. Many newbies (and some more experienced coders) spend many hours writing code to do the exact same things that they could do with a single call to DateDiff(). I hope to show you what this function is, how to use it, and how it can make your coding MUCH easier. .
content="text/html; charset=iso-8859-1">v:shapes="_x0000_s1027">
Daily Newbie
Off Right”
Edition
April 26,
2001
Freev:shapes="_x0000_s1027">
feature:
style="margin-left:135.0pt;text-indent:-135.0pt">face="Arial">Today’s Keyword:
size="4" face="Arial"> DateDiff()
style="margin-left:135.0pt;text-indent:-135.0pt">face="Arial">Name Derived
From:
"Date Difference "
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Used for
Determining the difference between two dates or times.
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">VB Help Description: Returns a Variant (Long) specifying the number of time intervals between two specified dates.
Plain
English: Makes adding and subtracting dates easier by allowing you to pass in a start and end date and get difference back. This difference can be in any valid date/time increment (day, week, month, quarter, year, hour, minute, second).
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Syntax: X = DateDiff(Interval, StartDateTime, EndDateTime)
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Usage: intDayCount = DateDiff("d","01/01/1995", "01/01/2001")
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Parameters:
yyyy =Year
q =Quarter
m =Month
y =Day of year
d =Day
w =Weekday
ww =Week
h =Hour
n =Minute
s =Second
style="margin-left:135.35pt;text-indent:-135.35pt">face="Arial">Copy & Paste Code:
style="margin-left:135.35pt;text-indent:-135.35pt"> size="2" face="Arial">
style="margin-left:1.25in;text-indent:.35pt;tab-stops:45.8pt 91.6pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">size="2" face="Arial">
Dim StartDate As Date
Dim EndDate As Date
Dim Interval As String
StartDate = InputBox ("Start Date:")
EndDate = InputBox ("End Date:")
Interval = InputBox ("Return In: s=seconds, m=Minutes h=Hours, d=Days, ww=Weeks, w=WeekDays, yyyy=years"
MsgBox DateDiff(Interval, StartDate, EndDate)
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Notes:
The DateDiff() function is one of the most useful ones VB has to offer. It literally replaces thousands of lines of code, takes in account leap years, knows how many days and weeks are in a month, and many other things that typically trip up home-brewed date code. Let's face it...those Microsoft guys can write some decent code. They went to a lot of trouble to create these functions in lower level languages so we could just call it and get a result back. Besides being much less likely to error out that your own code, it is also exponentially faster since it exists as true bytecode.
A couple of things to watch out for in the DateDiff() Function are:
Well I hope today's newsletter has helped save some newbie coders out there from pulling out clumps of hair over date manipulation. If you need more details on using DateDiff() please let me know.
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.