Windows Scripting
  Home arrow Windows Scripting arrow Page 5 - Working With Dates and Times in VBScript
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Working With Dates and Times in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-01-20

    Table of Contents:
  • Working With Dates and Times in VBScript
  • Retrieving the date and time
  • Working with date and time parts
  • Assigning Date values
  • Performing calculations
  • Date formats

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working With Dates and Times in VBScript - Performing calculations


    (Page 5 of 6 )

    Primarily, VBScript provides two different functions for performing Date and Time calculations: DateAdd, which is used to change a date based on a specified interval; and DateDiff, which is used to find the interval between two dates.

    WScript.Echo DateAdd("m", 1, Now)

    WScript.Echo DateAdd("d", -9, Date)

     

    Output:

    10/5/2008 4:34:36 AM

    8/27/2008

    DateAdd returns a Date value.  It accepts three parameters. The first is an interval string from Table 1 below; the second is a number indicating the number of intervals; and the third is a Date value.

    Table 4: Date interval strings

    “d”

    Day

    “m”

    Month

    “yyyy”

    Year

    “h”

    Hour

    “n”

    Minute

    “s”

    Second

    “w”

    Weekday

    “q”

    Quarter

    “y”

    Day of Year

    “ww”

    Week of Year

    You can see in the example above how I’ve used DateAdd to return dates based on today.  The first example returns the date one month from today.  The second example uses a negative value to return the date nine days ago.

    WScript.Echo DateDiff("d", "1-Apr-64", Now)

     

    Output:

    16228

    The DateDiff function returns the number of intervals between two dates.  It has three required parameters: the first is an interval string from Table 1 above, while the second and third are the two Date values to compare.  It accepts the same optional parameters as the DatePart function you saw earlier in this article.  In the example above, I’m using DateDiff to calculate the number of days between today’s date and April 1, 1964.

    WScript.Echo #2-Feb-2008# - #20-Jan-2008#

    WScript.Echo Date - #1-Jan-2008#

     

    Output:

    13.000000

    248.000000

    Finally, VBScript allows you to perform calculations on Dates directly.  If you recall, Dates are treated internally as Doubles.  As a result, VBScript will let you perform arithmetic calculations on them.

    Because the Date data type is a numeric type, Dates can be used in any of VBScript’s mathematical functions.

    Be aware that the result of arithmetic calculations between two or more dates will be of type Double.

    WScript.Echo Int(Now)

    WScript.Echo Fix(Now)

     

    Output:

    9/5/2008

    9/5/2008

    While Dates are used as Doubles for numeric calculations, I did notice one interesting behavior.  VBScript’s Int and Fix functions are used to return the integer portion of a floating number.  In both cases, VBScript correctly removes the time portion of a date, but returns the value as a Date instead of a Double.  Apparently, VB correctly recognizes that we supplied a Date value and kindly sends one back.  This got me wondering about VBScript’s behaviors.

    dtmNow = Now

    WScript.Echo dtmNow - Int(dtmNow)

    WScript.Echo dtmNow - Fix(dtmNow)

     

    Output:

    0.206192

    0.206192

    I then decided to see what would happen if I attempted to perform a calculation with those results.  In these examples, I’m using Int and Fix to return the date portion of the current date.  I’m then subtracting that from the result of the Now function which should return the current time.  In both cases, VBScript seems to return a Double.

    WScript.Echo TypeName(dtmNow - Int(dtmNow))

     

    Output:

    Double

    A quick check reveals that it does in fact return a Double.  So how about accuracy?

    WScript.Echo CDate(dtmNow - Int(dtmNow))

     

    Output:

    4:56:55 AM

    Sure enough, VBScript’s calculation correctly returns the current time.  This further proves how VBScript handles dates internally as numbers.

    More Windows Scripting Articles
    More By Nilpo


     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek