Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - 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 - Retrieving the date and time


    (Page 2 of 6 )

    VBScript provides three functions for retrieving the current system date and time.  While the current date and time are considered a single Date value, these functions can return either or both parts.  These values are based on the current system clock, so there can be extreme variances from system to system.

    WScript.Echo Now()

    WScript.Echo Date()

    WScript.Echo Time()

     

    Output:

    9/5/2008 12:59:01 AM

    9/5/2008

    12:59:01 AM

    The Now function returns the current system date and time.  The Date and Time functions return only those portions, respectively.  These functions return a Date data type.  Internally, this is handled as a 64-bit signed floating point number.  However, when displaying these values, VBScript will construct a string based on the local short-date and short-time formats as set by the system’s regional date and time settings.

    On my machine, you can see that the United States region uses an MM/DD/YYYY short-date format and a 12-hour HH:MM:SS short-time format.  The actual output you see may be different if you are using different regional date and time settings than I am.

    WScript.Echo CDbl(Now)

    WScript.Echo CDbl(Date)

    WScript.Echo CDbl(Time)

     

    Output:

    39696.040972

    39696.000000

    0.040972

    I mentioned that dates and times in VBScript are 64-bit signed floating point numbers.  In VBScript these are represented by the Double data type.  The code above is used to demonstrate what dates and times look like internally in VBScript.  As you can see, they are in fact a floating point number.  The integer portion is used to represent the date, while the decimal portion represents the time.  Supplying a zero value to one of these parts will cause it to be ignored.

    The current date and time is expressed as the number of days that have passed since midnight, December 30, 1899.  At the time of this writing, 36,696 days have passed since that time.  The time portion of this number expresses the current time at a fraction of a day.

    WScript.Echo Timer()

    VBScript also provides another useful function that is often little used.  The Timer function returns a Double value based on the current system time that indicates the number of seconds that have passed since midnight.  This can be used for performing calculations or timing events that require accuracy greater than a whole second.

    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