Home     Services     Tutorials     Pricing     Portfolio     Testimonials     Links     Contact    

 Tutorials
 Dreamweaver ASP
Insert, update and delete records in multiple tables from one form
Delete multiple records
Update multiple records

 ASP - VBScript
Format numeric values
Format Currency values
Working with dates and times

 ASP - Access
Retrieve Record Identity from an autonumber field on insert
Setting up an OLE DB connection string

 ASP - SQL Server
Retrieve record identity from an auto incremented field on insert
Generate a random number
Setting up an OLE DB connection string

Working with dates and timesWorking with dates and times

Working with dates and times

ASP / VBScript
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:

Visit the Microsoft Windows Script 5.6 Documentation download page.


Common parameters

DateAdd, DateDiff and DatePart.
Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week of year
h Hour
n Minute
s Second

DateDiff and DatePart only.
Constant Value Description
vbUseSystemDayOfWeek 0 Use National Language Support (NLS) API setting.
vbSunday 1 Sunday (default)
vbMonday 2 Monday
vbTuesday 3 Tuesday
vbWednesday 4 Wednesday
vbThursday 5 Thursday
vbFriday 6 Friday
vbSaturday 7 Saturday

DateDiff and DatePart only.
Constant Value Description
vbUseSystem 0 Use National Language Support (NLS) API setting.
vbFirstJan1 1 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.

Examples

datDate = DateAdd("m", 1, "31/01/2003")     ' datDate contains 28/02/2003
datYesterday = DateAdd("d", -1, Date())     ' datYesterday contains



DateDiff

DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])

Arguments

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.


Examples

datDaysToXmas = DateDiff("d", Date(), "")      ' datDaysToXmas =
datYrsFrom2000 = DateDiff("yyyy", "01/01/2000", Date())  ' datYrsFrom2000 =



DatePart

DatePart(interval, date[, firstdayofweek[, firstweekofyear]])

Arguments

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.


Examples

datThisDay = DatePart("d", Date())       ' datThisDay =
datThisMonth = DatePart("m", Date())     ' datThisMonth =
datThisYear = DatePart("yyyy", Date())   ' datThisYear =



FormatDateTime

FormatDateTime(Date[, NamedFormat])

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):

<%
datDate = Now()
datGeneralDate = FormatDateTime(datDate, 0)' datGeneralDate =
datLongDate = FormatDateTime(datDate, 1)   ' datLongDate =
datShortDate = FormatDateTime(datDate, 2)  ' datShortDate =
datLongTime = FormatDateTime(datDate, 3)   ' datLongTime =
datShortTime = FormatDateTime(datDate, 4)  ' datShortTime =
%>



You can also use the setLocale function to set the the LCID in order to display the dates in preferred format of different nationalities.

Here is the same example showing the current UK time display in US format:(mm/dd/yyyy):

<%
intLocale = SetLocale(1033)
datDate = Now()
datGeneralUS = FormatDateTime(datDate, 0)' datGeneralUS =
datLongUS = FormatDateTime(datDate, 1)' datLongUS =
datShortUS = FormatDateTime(datDate, 2)      ' datrShortUS =
datLongTimeUS = FormatDateTime(datDate, 3)   ' datLongTimeUS =
datShortTimeUS = FormatDateTime(datDate, 4)  ' datShortTimeUS =
%>


Click here to see a complete list of all the date formats in all the different national preference settings.