README.md
December 4, 2022 · View on GitHub
Description
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.
More Info
| Submitted On | |
| By | Matthew Roberts |
| Level | Beginner |
| User Rating | 4.8 (24 globes from 5 users) |
| Compatibility | VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access, VBA MS Excel |
| Category | Coding Standards |
| World | Visual Basic |
| Archive File |
Source Code
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).Syntax: X = DateDiff(Interval, StartDateTime, EndDateTime)
Usage: intDayCount = DateDiff("d","01/01/1995", "01/01/2001")
Parameters:
Copy & Paste Code:
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)
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.