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.

Daily Newbie - 04/25/2001

The
Daily Newbie

“To Start Things
Off Right”

Third
Edition
April 26,
2001
Free

About this
feature:

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.

.

Today’s Keyword: DateDiff()

Name Derived
From:"Date Difference "

Used for
Determining the difference between two dates or times.

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).

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:

Times can mess you up. When you call DateDiff without specifying a time (i.e. "01/10/200" instead of "01/01/2000 9:25:00"), DateDiff assumes a time of midnight (00:00:01). This can have the effect of "skipping" a day if you aren't careful. Check your results a few times and adjust your dates or times to make it right. Once you have it, it will always work the same.

Switching dates will return negative values. Not a tragedy, but something you should be aware of.

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.

Use this form to tell us if this entry should be deleted (i.e contains
no code, is a virus, etc.).This submission should be removed because:

Your Vote

What do you think of this article (in the
Beginner category)?(The article with your highest vote will win this month's coding contest!)
Excellent Good Average Below Average Poor
(See voting log ...)

Other User Comments

There are no comments on this submission.

Add Your Feedback

Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)

My nonprogramming sites: The Real Estate Crowdfunding Review: Investing tools that cut through the clutter and the hype.
Real information and reviews. Real-time investment feeds. Real profitable. | Dollar candy: Play 60 second puzzles against three other people for real money! Legal in 36 states and overseas.