Tools Links Login

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



content="text/html; charset=iso-8859-1">
Daily Newbie - 04/25/2001



v:shapes="_x0000_s1027">


The
Daily Newbie


“To Start Things
Off Right”


Third
Edition          
                  
April 26,
2001          
                        
Free


v:shapes="_x0000_s1027">




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.


.


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:                    

  • Interval - The type of results you want returned. These are:

    yyyy =Year
    q =Quarter
    m =Month
    y =Day of year
    d =Day
    w =Weekday
    ww =Week
    h =Hour
    n =Minute
    s =Second



  • StartDateTime - Any valid date, time, or datetime combination. Examples: "01/01/2000" , "01/01/2000 12:25 AM" , "16:30"
  • EndDateTime - Same criteria as StartDateTime. This is the data the start date will be subtracted from.


    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:


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




  • About this post

    Posted: 2002-06-01
    By: ArchiveBot
    Viewed: 86 times

    Categories

    Visual Basic 6

    Attachments

    No attachments for this post


    Loading Comments ...

    Comments

    No comments have been added for this post.

    You must be logged in to make a comment.