Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - 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 - Assigning Date values


    (Page 4 of 6 )

    VBScript provides support for performing calculations on dates and times and provides a number of functions to that end.  Before you learn how to perform these calculations, it’s important to understand how to assign date and time values.

    dtmValue = #1/31/1976#

    dtmValue = #31-1-1976#

    dtmValue = #January 31, 1976#

    dtmValue = #Jan 31 1976#

    dtmValue = #3:01 PM#

    You’ve already seen how the Now, Date, and Time functions can be used to assign Date values.  It’s important to realize that you can also assign Date values literally using the number symbol in much the same way as a double quotation mark indicates a string.  Any acceptable time or date format can be used, and VBScript doesn’t care whether you use a forward slash or a hyphen to separate date parts.

    Leaving any portion of the date or time unspecified will assume the current day.  For example, assigning January 2 to a date will assume January 2 of the current year.

    Be very careful when literally assigning dates using the short-date format.  VBScript is smart enough to realize there cannot be 31 months in a year, and it will correctly assign that to the day portion; however, using numbers 12 and below gets confusing.  VBScript assumes that the local short-date format is used.  Thus, providing the date 1/4/2008 in the United States will assume MM/DD/YYYY format and assign the date January 4, 2008 when you may have intended April 1, 2008 instead.

    Assigning an invalid date (such as February 29, 1900—since 1900 was not a leap year) will result in a general syntax error.  This can make debugging a bit rough.  If you are assigning a date based on user input, it’s a good idea to validate that information to prevent errors.

    dtmDate = DateSerial(1976, 1, 31)

    dtmTime = TimeSerial(13, 30, 0)

    VBScript also provides two functions for creating Date type values based upon integer part values.  The DateSerial function returns a Date.  It accepts three parameters: a four-digit year, an integer month, and an integer day, respectively.  The TimeSerial function works similarly and accepts an integer hour (based on the 24-hour clock), an integer for minutes, and an integer for seconds.

    dtmDate = DateValue("January 31, 1976")

    dtmDate = CDate("January 31, 1976")

     

    dtmTime = TimeValue("3:30 PM")

    dtmTime = CDate("3:30 PM")

    VBS provides two more useful functions for assigning Dates.  The DateValue and TimeValue both create Date types based on formatted strings.  Additionally, the CDate function can be used to convert any sub-type into a Date type.  An error will occur if the supplied value cannot be interpreted as a Date.

    WScript.Echo CDate("January 31, 1976 3:30 PM")

    WScript.Echo DateValue("January 31, 1976 3:30 PM")

    WScript.Echo TimeValue("January 31, 1976 3:30 PM")

     

    Output:

    1/31/1976 3:30:00 PM

    1/31/1976

    3:30:00 PM

    The reasons for using DateValue and TimeValue to convert a string instead of CDate may not be immediately obvious.  Consider the example above.  CDate is creating a Date value for the entire supplied string.  DateValue and TimeValue will allow you to create Date values containing only the specified portion of the string while ignoring the rest.

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek