|
Date
variables
Using
a date data type allows you to store a date-time entity with
a value from midnight on 1. January, AD 100 until midnight
at the end of 31. December, 9999.
Convention
is that date-time variable names begin with the prefix dtm,
as in
dtmStartDate
dtmBirthday
dtmZeroHour
(This
is a convention, it's optional.)
Assigning
a literal date or time to a date-time type variable uses the
hash symbol, #, rather than a quote character as the delimiter
dtmMillenium
= #January 1, 2000#
dtmZeroHour
= #February 17, 1802 23:39#
dtmNearlyMidnight
= #March 1, 2005 11:59PM#
Note
that American date conventions have been used in these examples
and that 12-hour and 24-hour time formats are both allowed.
The sysem locale and the system's date format determine how
VBScript handles dates. If the time is not specified then
it is set to all zeros, 00:00 at the start of the day specified
in the date. (This has implications when performing arithmetic
on dates.)
Variables
having a string data type can be converted to a date-time
type using CDate,
as in
strStringDate
= "January 1, 2010"
dtmDateType = CDate(strStringDate)
This
is, of course, equivalent to
dtmDateType
= #January 1, 2010#
A
number of key date functions are discussed in the example
of determining the number
of days in a month. |
How
dates are stored
VBScript
stores dates in exactly the same way that Access does. Both
VBScript and Access utilise the same COM structure to store
date-time data. This is known as a vtDate Variant.
A
VtDate is represented by a double-precision floating point
number.
The
integer portion (to the left of the decimal point) represents
the day number - A series of day numbers beginning on 30.
December 1899, with day +1 being 31. December 1899, as here:
dateSerial(Year(CDate(1)),
_
Month(CDate(1)), _
Day(CDate(1)))
=
31/12/1899
(Notice
that when all is zero you can get some odd results, like this
dateSerial(Year(CDate(0)),
_
Month(CDate(0)), _
Day(CDate(0)))
=
00:00:00
which
is a good case for using formatted dates in output)
The
mantissa portion of the double precision floating point number
(to the right of the decimal point) is the representation
of the time during the day, some key values being
| zero |
00:00:00 |
midnight |
| .5 |
12:00:00 |
midday |
| .75 |
18:00:00 |
six
in the evening |
With
1440 minutes in a day, a minute is 1/1440, or
6.94444444444444E-04
, as the system would have it. |
Back
to the main article

|
| |
response.write
"March begins on " & _
dtmFirstDay & _
" and ends on " & _
dtmLastDay
result
in the output
March
begins on March 1, 2005 and ends on March 31, 2005
Adding
and subtracting fractions of one works in a similar way, manipulating
the time relative to the date-time value. Bearing in mind
there are 1440 minutes in a day, using dtmValue
+ (60 / 1440) advances the variable an hour, for instance
dtmMidday
= #May 2, 2004 12:00#
dtmTeaTime = dtmMidday + (360 / 1440)
where
(360 / 1440) is 360 minutes, or six hours: Teatime is at six
o'clock in the evening. |
| |
If
you would rather use the native date functionality then you
will need to determine what settings are in place.
This
can be done by reference to constants set by the VBScript
runtime environment. So, for instance, to return a day of
the week (the number of the day, starting with day one) you
can use
Weekday(dtmDate)
If
the date is the first day of the week it will return one,
the next day 2 and so on. To determine which day of the week
we are dealing with (Monday, Tuesday, Wednesday) we need to
know on which day the server's week begins. We can use the
constants vbSunday,
vbMonday and
so on (see the example) to
make a comparison with the returned value or vbXxxxx
to determine the weekday settings. Months are more straightforward,
with January always being the first! |