Built-in date functions

VBScript provides number of built-in date functions to find time, and manipulate dates and time. For example, to get current date, you can use the date () function. (Please note the examples on this page use the date/time on the server, which may not be the same as your local date or time!)

<%
response.write "Today is " & date ()
%>

That returns

Today is 12/15/2018

To find the current time, you can use the Time () function, as:

<%
response.write "Current time is " & Time ()
%>

That will return:

Current time is 10:39:57 PM

If you need to get both the date and time, you can use the now () function. The now () function returns current date and time, for example

<%
response.write "Now is " & Now ()
%>

returns

Now is 12/15/2018 10:39:57 PM

Manipulating dates and time

So now you know how to get the current date and time; let's next cover how to manipulate dates and time data. With the DateAdd () function, we can add time interval values to a specific time or date. The general syntax for using DateAdd () is DateAdd (interval, number, date); where interval is a string that represents time interval values (see table 1), number is the number of intevals (like year, quarter, month, etc.) you want to add, and date is a date value.

Table 1 Interval values for DateAdd ()
Interval Value Represents
"yyyy" Year
"q" Quarter
"m" Month
"d" Day
"ww" Week (week of year; from 1 to 52)
"h" Hour
"n" Minute
"s" Second

Table 2 shows examples of DateAdd () function in use.

Table 2 DateAdd () function in use
Code Output Explanation
response.write dateadd("yyyy", 1, date) 12/15/2019 Adds 1 year to current year.
response.write dateadd("q", 2, date) 6/15/2019 Adds 2 more quarters to current quarter.
response.write dateadd("m", 5, date) 5/15/2019 Adds 5 more months to current month.
response.write dateadd("ww", 10, date) 2/23/2019 Adds 10 more weeks to the current week of year.
response.write dateadd("h", 5, time) 12/31/1899 3:39:57 AM Displays time 5 hours from now.
response.write dateadd("n", 53, time) 11:32:57 PM Displays time 53 minutes from now.
response.write dateadd("s", 10, time) 10:40:07 PM Adds 10 seconds to current time.

So you may agree adding date/time values is easy with the DateAdd () function. But what if you wanted to subtract date/time values, is there a VBSCript date function to do that? Yes, the DateDiff () function finds the time between two dates. The general syntax for using DateDiff () function is DateDiff (interval, date1, date2); where interval is one of the interval values shown in table 3, and date1 and date2 represents some date or time values.

Table 3 Interval values for DateDiff ()
Interval Value Represents
"yyyy" Year
"q" Quarter
"m" Month
"d" Day
"ww" Week (week of year; from 1 to 52)
"h" Hour
"n" Minute
"s" Second

Table 4 shows examples of DateDiff () in use. Notice that the date values in the table are surround by the pound signs (#s) to avoid division. Also, note the use of timeserial () function, called with a specific hour, minute and second values.

Table 4 DateDiff () function in use
Code Output Explanation
response.write DateDiff("yyyy", #1/1/2000#, date) 18 Number of years since 2000.
response.write DateDiff("q", date, #1/1/2080#) 245 Number of quarters to the first quarter of 2080.
response.write DateDiff("m", #1/1/1800#, date) 2627 Number of months since 1800.
response.write DateDiff("ww", #12/1/2000#, #12/10/2000#) 2 Number of calendar weeks between 12/1/2000 and 12/10/2000.
response.write DateDiff("h", timeserial (0,0,0), timeserial (5,50,0)) 5 Difference, in hours, between 12:00:00 AM and 5:50:00 AM.
response.write DateDiff("n", timeserial (0,0,0), timeserial (0,50,0)) 50 Difference, in minutes, between 12:00:00 AM and 12:50:00 AM.
response.write DateDiff("s", timeserial (0,0,0), timeserial (0,50,0)) 150 Difference, in seconds, between 12:00:00 AM and 12:02:30 AM.

The timer () built-in function returns number of seconds since midnight. This function can be useful to determine the amount of time it takes to execute some code. For example, the following shows how many seconds the loop will take to complete:

dim intStartTime, intEndTime, intSecondsPassed, intEventNumberSum
intStartTime = timer ()
response.Write "Seconds since midnight, before the loop starts: " & intStartTime
intEventNumberSum = 0
for i = 0 to 10000
select case isEven(i)
case -1
case 0
intEventNumberSum = intEventNumberSum + i
end select
next
response.write "<br>Sum of even numbers 0 - 10000 is " & intEventNumberSum
intEndTime = timer ()
response.Write "<br>Seconds since midnight after the loop is complete: " & _ intEndTime
intSecondsPassed = intEndTime - intStartTime
response.write "<br>Time taken to complete the for loop: " & intSecondsPassed
function isEven (num)
dim result
result = num mod 2
select case result
case 1
isEven = -1
case 0
isEven = result
end select
end function

In this example, we use a for loop to find the sum of even numbers between 0 and 10000. Computing this calculation will take some time and we'll use the timer () function to determine how long exactly this loop takes to perform the calculations.

So before we execute the loop, we call the timer () function (see line 2) to get time in seconds since 12:00 AM. The next line prints this time. The variable intEventNumberSum stores the sum of even numbers (see line 9: intEventNumberSum = intEventNumberSum + i). In the for loop, we call the isEven (i) function and pass the current value of i. This function returns 0 if the value of i is a even number; otherwise -1.

After the loop is done, we call the timer () function again; see line 13: intEndTime = timer (). Note with this function call more time has passed since we last called this function, line 2. In other words, the value of intStartTime is likely to be less than (or perhaps equal to intEndTime if our loop completes immediately) intEndTime. To determine the difference between these two variables, we subtract intStartTime from intEndTime (see line 15: intSecondsPassed = intEndTime - intStartTime).

The following shows the output of the above code:

Seconds since midnight, before the loop starts: 81597.66
Sum of even numbers 0 - 10000 is 25005000
Seconds since midnight after the loop is complete: 81597.66
Time taken to complete the for loop: 0

Retrieving date/time values

When programming, often you just want to retrieve just the current month, year, day or some other specific time, but not the rest of date information returned by date functions available in VBScript. With DatePart (), you can retrieve only a specific part of a date/time value. For instance, you can retrieve the current year, quarter, month, day, week of the year (from 1 to 52), hour (from 0 to 23), minute, or second. The general syntax for calling DatePart () is

DatePart(IntervalValue, dateValue)

The first argument above represents the specific value you want to retrieve (see table 5 for possible interval values for the first argument) from a specific date or time (argument 2).

Table 5 Interval values for DatePart ()
Interval Value Represents
"yyyy" Year
"q" Quarter
"m" Month
"d" Day
"ww" Week (week of year; from 1 to 52)
"h" Hour
"n" Minute
"s" Second

See table 6 for some examples.

Table 6 DatePart () function in use
Code Output Explanation
response.write DatePart("yyyy", date) 2018 Returns current year.
response.write DatePart("q", date) 4 Returns current quarter.
response.write DatePart("m", date) 12 Returns current month.
response.write DatePart("ww", date) 50 Returns week of the year (from 1 to 52).
response.write DatePart("h", time) 22 Returns current hour.
response.write DatePart("n", time) 39 Returns current minute from the time () function.
response.write DatePart("s", time) 57 Returns current second from the time () function.

VBScript also provides number of other functions that provide specific date or time values. See table 7 for a summary of these functions.

Table 7 functions for retrieving time and date values
Function Description
Year (date) Returns the year portion from date.
Month (date) Returns the month portion from date.
MonthName (MonthInt) Returns a string with the name of the function. This function also allows you to pass the second argument although it is optional. The second argument indicates whether you want the string name of the function as a abbreviation or the whole month. The default is false, which means the whole name of the month is returned; if true is specified as the second argument, a abbreviated name of the month is returned.
Day (date) Returns the day portion from date.
Hour (time) Returns the hour portion from time.
Minute (time) Returns the minute portion from time.
Second (time) Returns the second portion from time.

See table 8 for examples of the functions mentioned in table 7.

Table 8 examples of using functions described in table 7
Code Output Explanation
<%= Year (date) %> 2018 Returns current year.
<%= Month (date) %> 12 Returns current month.
<%= MonthName (12) %> December Returns the full name of the month.
<%= MonthName (12, false) %> December Also returns the full name of the month.
<%= MonthName (12, true) %> Dec Returns abbreviated name of the month.
<%= Day (date) %> 15 Returns day portion from the date.
<%= Hour (time) %> 22 Returns the hour portion from time.
<%= Minute (time) %> 39 Returns the minute portion from time.
<%= Second (time) %> 57 Returns the second portion from time.