VBScript has a number of useful functions allowing you manipulate and make calculations
involving dates and times.
Sections DateAdd - used to add or subtract a specified
time interval from a date. DateDiff - used to determine how many specified
time intervals exist between two dates. DatePart - used to to evaluate a date and return
a specific interval of time. FormatDateTime - used to alter the way a
date or time value. In conjunction with the SetLocale function the
dates and times can be displayed in different formats according to
national preference, dd/mm/yyyy, mm/dd/yyyy etc.
Here are some extracts from the Microsoft Windows Script 5.6 Documentation:
Start with the week in which January 1 occurs (default).
vbFirstFourDays
2
Start with the week that has at least four days in the
new year.
vbFirstFullWeek
3
Start with the first full week of the new year.
DateAdd
DateAdd(interval, number, date)
Arguments
interval
Required. String expression that is the interval you want to add. See Settings
section for values.
number
Required. Numeric expression that is the number of interval you want to add.
The numeric expression can either be positive, for dates in the future, or negative,
for dates in the past.
date
Required. Variant or literal representing the date to which interval is added.
You can use the DateAdd function to add or subtract a specified time interval
from a date. The DateAdd function will only return valid dates.
interval
Required. String expression that is the interval you want to use to calculate
the differences between date1 and date2. See Settings section for values.
date1, date2
Required. Date expressions. Two dates you want to use in the calculation.
firstdayofweek
Optional. Constant that specifies the day of the week. If not specified, Sunday
is assumed. See Settings section for values.
firstweekofyear
Optional. Constant that specifies the first week of the year. If not specified,
the first week is assumed to be the week in which January 1 occurs. See Settings
section for values.
Remarks
You can use the DateDiff function to determine how many specified time intervals
exist between two dates. For example, you might use DateDiff to calculate the
number of days between two dates, or the number of weeks between today and the
end of the year.
To calculate the number of days between date1 and date2, you can use either Day
of year ("y") or Day ("d"). When interval is Weekday ("w"),
DateDiff returns the number of weeks between the two dates. If date1 falls on
a Monday, DateDiff counts the number of Mondays until date2. It counts date2
but not date1. If interval is Week ("ww"), however, the DateDiff function
returns the number of calendar weeks between the two dates. It counts the number
of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday;
but it doesn't count date1, even if it does fall on a Sunday.
If date1 refers to a later point in time than date2, the DateDiff function returns
a negative number.
The firstdayofweek argument affects calculations that use the "w" and "ww" interval
symbols.
If date1 or date2 is a date literal, the specified year becomes a permanent part
of that date. However, if date1 or date2 is enclosed in quotation marks (" ")
and you omit the year, the current year is inserted in your code each time the
date1 or date2 expression is evaluated. This makes it possible to write code
that can be used in different years.
When comparing December 31 to January 1 of the immediately succeeding year, DateDiff
for Year ("yyyy") returns 1 even though only a day has elapsed.
interval
Required. String expression that is the interval of time you want to return.
See Settings section for values.
date
Required. Date expression you want to evaluate.
firstdayofweek
Optional. Constant that specifies the day of the week. If not specified, Sunday
is assumed. See Settings section for values.
firstweekofyear
Optional. Constant that specifies the first week of the year. If not specified,
the first week is assumed to be the week in which January 1 occurs. See Settings
section for values.
Remarks
You can use the DatePart function to evaluate a date and return a specific interval
of time. For example, you might use DatePart to calculate the day of the week
or the current hour.
The firstdayofweek argument affects calculations that use the "w" and "ww" interval
symbols.
If date is a date literal, the specified year becomes a permanent part of that
date. However, if date is enclosed in quotation marks (" "), and you
omit the year, the current year is inserted in your code each time the date expression
is evaluated. This makes it possible to write code that can be used in different
years.
Settings
The NamedFormat argument has the following settings:
Constant
Value
Description
vbGeneralDate
0
Display a date and/or time. If there is a date part,
display it as a short date. If there is a time part, display it as a long
time. If present, both parts are displayed.
vbLongDate
1
Display a date using the long date format specified in
your computer's regional settings.
vbShortDate
2
Display a date using the short date format specified
in your computer's regional settings.
vbLongTime
3
Display a time using the time format specified in your
computer's regional settings.
vbShortTime
4
Display a time using the 24-hour format (hh:mm).
Examples (using
English - United Kingdom format and current UK time):