Working with Dates and Time


© Maxim Karetnikov

Date information can be an important part of a script. Perhaps your script receives an order request date from a user. Maybe it collects an appropriate time schedule for some meeting. These tasks would be fairly difficult to perform if a date was simply a string like November 7, 1917 or 06:22:11. Luckily, VBScript provides you with a ready toolkit for working with date and time information.

Many of the VBScript functions can work with a date, a time, or a time and date. In VBScript, dates and times are often stored together. The date subtype is viewed as representing both a date and time in its own format.

Since all variables in VBScript are variants, date and time values are stored as variants with a subtype of date. The function CDate recognizes date and time literal as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight. The following example uses the CDate function to convert a string to a date.

MyDate = "October 19, 1962" ' Define date.
VBDate = CDate(MyDate) ' Convert to Date data type.
MyTime = "4:35:47 PM"         ' Define time.
VBTime = CDate(MyTime)'  Convert to Date data type.

In general, hard coding dates and times as strings (as shown in this example) is not recommended. Use date and time literal (such as #10/19/1962#, #4:45:23 PM#) instead.

VBScript can explicitly convert string to a date or time, using either the DateValue() function for a date, or the TimeValue() function for a time expression. DateValue() function has the following syntax:

DateValue(date)

The date argument is normally string expression representing a date from January 1, 100 through December 31, 9999. However, date can also be any expression that can represent a date, a time, or both a date and time, in that range. If the date argument includes time information, DateValue doesn't return it. However, if date includes invalid time information (such as "89:98"), an error occurs.

If date is a string that includes only numbers separated by valid date separators, DateValue recognizes the order for month, day, and year according to the short date format you specified for your system. DateValue also recognizes unambiguous dates that contain month names, either in long or abbreviated form. For example, in addition to recognizing 12/30/1991 and 12/30/91, DateValue also

Go To Page: 1 2


The copyright of the article Working with Dates and Time in VB Script is owned by . Permission to republish Working with Dates and Time in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo


Here's the follow-up discussion on this article: View all related messages

3.   Oct 25, 2002 11:01 AM
Hello All,
Please Could anybody help me with this :
I have this Outlook 2000 application development problems which I
dont know how to begin with as even though I've been programming with
other ...

-- posted by akidi


2.   Sep 7, 2001 6:29 AM
In response to message posted by mdehnel:

Thank you for your question. You can combine date and time in one string using string co ...


-- posted by kmaxim


1.   Aug 23, 2001 7:15 AM
Is there a proper way to combine date and time

If I have strings for
mydate August 23, 2001
and
mytime 1:20:00 AM
can I combine them into 1?

I get and error when I try
CDate(08/23/20 ...


-- posted by mdehnel





For a complete listing of article comments, questions, and other discussions related to Maxim Karetnikov's VB Script topic, please visit the Discussions page.