, 08/04/2007 20:20:15. FormatDateTime(Now(),vbLongDate)%>, 08 April 2007. FormatDateTime(Now(),vbShortDate)%>, 08/04/.">
Skip to content

Vbscript Formatdatetime Formats For Essays

VBScript date and time

VBScript provides the functionality to display date and time on a webpage.

NOTE: Whenever you display the date or time on a webpage using VBScript, it will get that data from whatever the user has on their computer, whether it is correct or not. So if the current date and time is January 15th 12:30, but the users computer is set to January 10th 8:30, that is what will be displayed.

NOTE: To see the output for the examples on this page look at the page in Internet Explorer or on Firefox using the IETab extension.

This tutorial focuses on:

  • VBScript date functions
  • VBScript time functions
  • Displaying a date on a webpage
  • Displaying time on a webpage
  • Displaying date and time on a webpage
  • Displaying custom formatted date and time

VBScript date functions

VBScript provides several functions for displaying a date on a webpage.

The Date function

The Date function is used to return the current date.

Example:

<script type="text/vbscript"> document.write("The current date is " & Date) </script>

The Day() function

The date parameter is any variable or function that represents a date. This function will return the day of the month (in numerical form from 1 to 31) from the date specified by the date parameter.

Example:

<script type="text/vbscript"> document.write("Current date: " & Date & "<br />") 'get the current day of the month from the current date document.write("<br />The current day of the month is " & Day(Date)) </script>

The Month() function

The date parameter is any variable or function that represents a date. This function will return the current month (in numerical form from 1 to 12) from the date specified by the date parameter.

Example:

<script type="text/vbscript"> document.write("Current date: " & Date & "<br />") 'get the current month from the current date document.write("The current month is " & Month(Date)) </script>

The Year() function

The date parameter is any variable or function that represents a date. This function will return the current year (in numerical form (4 digits)) from the date specified by the date parameter.

Example:

<script type="text/vbscript"> document.write("Current date: " & Date & "<br />") 'get the current year from the current date document.write("The current year is " & Year(Date)) </script>

The IsDate() function

The date parameter is any expression or function that can be checked if it is a date or not. This function will return a boolean value (true/false) signifying if date is a date or not.

Example:

<script type="text/vbscript"> document.write(IsDate(Date) & "<br />") document.write(IsDate("Apple") & "<br />") document.write(IsDate("January 1, 1900") & "<br />") document.write(IsDate("January first nineteen hundred")) </script>

VBScript time functions

VBScript provides several functions for displaying time on a webpage.

The Time function

The Time function is used to return the current time.

Example:

<script type="text/vbscript"> document.write("The time is now " & Time) </script>

The Minute() function

The time parameter is any variable or function that represents time. This function will return the current minute (from 0 to 59) from the time specified by the time parameter.

Example:

<script type="text/vbscript"> document.write("Current time: " & Time & "<br />") 'get the current minute from the time specified document.write("The current minute is " & Minute(Time)) </script>

The Hour() function

The time parameter is any variable or function that represents time. This function will return the current hour (from 0 to 23) from the time specified by the time parameter.

Example:

<script type="text/vbscript"> document.write("Current time: " & Time & "<br />") 'get the current hour from the time specified document.write("The current hour is " & Hour(Time)) </script>

The Second() function

The time parameter is any variable or function that represents time. This function will return the current second (from o to 59) of the current minute from the time specified.

Example:

<script type="text/vbscript"> <!-- document.write("Current time: " & Time & "<br />") 'get the current second from the current minute from the time specified document.write("The current second is " & Second(Time)) --> </script>

The Timer function

The Timer function is used to return the number of seconds that have elapsed since midnight (12 a.m.) for the current day.

Example:

<script type="text/vbscript"> document.write("Current time: " & Time & "<br />") document.write("Number of seconds elapsed in the current day: " & Timer) </script>

Displaying a date on a webpage

You can display a date on a webpage using the above mentioned date functions:

Example:

<script type="text/vbscript"> document.write("Current date: " & Date & "<br />") document.write("Current month: " & Month(Date) & "<br />") document.write("Current day: " & Day(Date) & "<br />") document.write("Current year: " & Year(Date) & "<br />") document.write("'January first eighteen hundred' a valid date format: " & IsDate("January first eighteen hundred")) </script>

Displaying time on a webpage

You can display time on a webpage using the above mentioned time functions:

Example:

<script type="text/vbscript"> document.write("Current time: " & Time & "<br />") document.write("Current hour: " & Hour(Time) & "<br />") document.write("Current minute: " & Minute(Time) & "<br />") document.write("Current second: " & Second(Time) & "<br />") document.write("Number of seconds elapsed since current day began: " & Timer) </script>

Displaying date and time on a webpage

You can display the date and time together on a webpage using the Now function:

Example:

<script type="text/vbscript"> document.write("Date and time: " & Now) </script>

Displaying custom formatted date and time

VBScript displays date and time in one format by default but you can change this using the FormatDateTime() function.

Syntax:

FormatDateTime(dateOrTime, format)

The dateOrTime parameter is the date or time you want to format, and the format parameter is the type of formatting to apply.

The format parameter can take a value from 0 - 4, each value signifying a different type of formatting:

0 (used for dates) - display a date in the format mm/dd/yy (default)
1 (used for dates) - display a date in the format day, month and month day, year
2 (used for dates) - display a date in the format mm/dd/yy (default (same as 0))
3 (used for time) - display time in the format hh:mm:ss: PM/AM (12 hour format)
4 (used for time) - display time in the format hh:mm (24 hour format)

Example:

<script type="text/vbscript"> document.write("Current date: " & FormatDateTime(Date, 0) & "<br />") document.write("Current date: " & FormatDateTime(Date, 1) & "<br />") document.write("Current date: " & FormatDateTime(Date, 2) & "<br />") document.write("Current time: " & FormatDateTime(Time, 3) & "<br />") document.write("Current time: " & FormatDateTime(Time, 4)) </script>

This topic explains how to work with date values in VBScript and gives examples of date operations. It contains the following sections:

Basics

When writing scripts we often deal with dates. There are certain date value formats and TestComplete routines that help handle dates.

Since the TestComplete scripting engine only supports OLE-compatible data types, the date-time values are implemented as floating-point variant values in a special format. The integer part of this value represents the number of days that have passed since December 30, 1899. The number after the decimal separator represents the fraction of a 24-hour period that has elapsed. However, you do not have to understand what these floating-point values represent. TestComplete provides several routines that help you convert these values to their string representation (see below).

Below are some examples of date-time values and their meaning:

0.25December 30, 1899. 6:00 AM
36345.5July 4, 1999. 12:00 PM
39094.65625January 12, 2007. 3:45 PM

When you are only working with date values the fractional part can be omitted.

Objects and Functions for Working With Date Values

TestComplete has the object that contains methods that can be useful when operating with dates.

AddDaysAdds or subtracts the specified number of days to (from) the given date.
AddMonthsAdds or subtracts the specified number of months to (from) the given date.
AddTimeAdds or subtracts the specified number of days, hours, minutes and seconds to (from) the given date.
CompareCompares two specified date/time values.
GetDayReturns the ordinal number of a day in a month.
GetDayOfWeekReturns the day of the week for the specified date.
GetDayOfYearReturns the ordinal number of a day in a year.
GetMonthReturns the month number of the specified date.
GetYearReturns the year number of the specified date.
IsLeapYearIndicates whether the specified year is a leap year.
NowReturns the current date and time.
SetDateElementsReturns the Date variable having the specified year, month and day.
SetDateTimeElementsReturns the Date variable having the specified date and time portions.
SetSystemDateTimeAssigns the specified date and time as the system date and time.
TodayReturns the current date.

One more object, , provides methods to convert strings between date values and their string equivalents:

The and objects are available for all supported scripting languages, so that you can use them to operate with date values regardless of the chosen language.

You can also use these and other built-in VBScript functions in your scripts. Here is a list of built-in functions that can be useful when dealing with dates:

CDateReturns an expression that has been converted to a Variant of subtype Date
DateReturns the current system date. (See a note below to learn about the format of date values in VBScript.)
DateAddReturns the date to which the specified time interval has been added.
DateDiffReturns the number of intervals between two dates.
DatePartReturns the specified part of a given date.
DateSerialReturns a Variant of subtype Date for the specified year, month, and day.
DateValueReturns a Variant of subtype Date.
DayReturns a whole number between 1 and 31, inclusive, representing the day of the month.
FormatDateTimeReturns an expression formatted as a date or time.
IsDateReturns a Boolean value indicating whether an expression can be converted to a date.
MonthReturns a whole number between 1 and 12, inclusive, representing the month of the year.
MonthNameReturns a string indicating the specified month.
NowReturns the current date and time according to the date and time set on your computer.
WeekdayReturns a whole number representing the day of the week.
WeekdayNameReturns a string indicating the specified day of the week.
YearReturns a whole number representing the year.
Note: Native VBScript date/time values are stored as strings in the #MM/DD/YYYY HH:MM:SS PM/AM# format. (For example, #1/25/2005 2:17:38 PM#.) These strings are automatically converted to the floating-point variant format when required. In most cases, you can pass a VBScript date to a TestComplete routine or vice versa and get the correct results. However, the initial representation of VBScript date/time values should be kept in mind.

Getting Current Date

There are two routines that return current date: and . The difference between them is that the value returned by the routine includes both the date and time parts, whereas the routine returns only the date part. The script below demonstrates how to use them.

VBScript

Getting Tomorrow’s and Yesterday’s Dates

The samples below demonstrate how the object can be used to calculate tomorrow's date. The current date is obtained via the method. Then the current date value is incremented via the method. The method of the object is used to convert the date value to a string that is posted to the TestComplete log.

In a similar way you can calculate any date that differs from the current day by a certain number of days: yesterday, the next or previous week and so on. For example to get yesterday’s date, you have to pass the -1 value to the method as the second (Days) parameter.

VBScript

Function TomorrowDate
' Obtain the current date
  CurrentDate = aqDateTime.Today

' Convert the date/time value to a string and post it to the log
  Today = aqConvert.DateTimeToStr(CurrentDate)
  Log.Message("Today is " + Today)

' Calculate the tomorrow’s date, convert the returned date to a string and post this string to the log
  TomorrowDate = aqDateTime.AddDays(CurrentDate, 1)
  ConvertedTomorrowDate = aqConvert.DateTimeToStr(TomorrowDate)
  Log.Message("Tomorrow will be " + ConvertedTomorrowDate)
EndFunction

VBScript

Function YesterdayDate
' Obtain the current date
  CurrentDate = aqDateTime.Today

' Convert the date/time value to a string and post it to the log
  Today = aqConvert.DateTimeToStr(CurrentDate)
  Log.Message("Today is " + Today)

' Calculate the yesterday’s date, convert the returned date to a string and post this string to the log
  YesterdayDate = aqDateTime.AddDays(CurrentDate, -1)
  ConvertedYesterdayDate = aqConvert.DateTimeToStr(YesterdayDate)
  Log.Message("Yesterday was " + ConvertedYesterdayDate)
EndFunction

Comparing Dates

To compare date/time values, use the method rather than comparison operators (like =, >, <, <> and others) provided by the scripting engine:

VBScript

r = aqDateTime.Compare(DateTime1, DateTime2)
If r > 0 Then
' DateTime1 > DateTime2
Else
If r < 0 Then
' DateTime1 < DateTime2
Else
' DateTime1 = DateTime2
EndIf
EndIf

Calculating the Year and Month Duration

Generally there are 365 days in a calendar year. However the length of an astronomical year is about 365.242375 days, so in the course of time the fractional part would result in an extra day and the calendar year would lag from the astronomical. To avoid this the leap year that is 366 days long was invented.

The Gregorian calendar is the current standard calendar in most of the world and it adds a 29th day to February in all years evenly divisible by 4, except for centennial years (those ending in -00), which only receive the extra day if they are evenly divisible by 400. The object has a special method ,, that accepts the year number and returns true if the specified year is a leap year. We can use this method to get the exact number of days in the desired year:

Using the method we can get the number of days in the month. For all months, except February, the duration of a month is fixed and is either 30 or 31 days. The duration of February is 28 or 29 days depending on whether the current year is a leap year or not. The routine that gets the month duration would be the following:

VBScript

Function DaysInYear(YearNo)
If aqDateTime.IsLeapYear(YearNo) Then DaysInYear=366 Else DaysInYear=365
EndFunction

VBScript

Function DaysInMonth(MonthNo,YearNo)
SelectCase MonthNo
Case 1,3,5,7,8,10,12 DaysInMonth=31
Case 2 If aqDateTime.IsLeapYear(YearNo) Then DaysInMonth=29 Else DaysInMonth=28
Case 4,6,9,11 DaysInMonth=30
EndSelect
EndFunction

Encoding and Decoding Date Values

Since date values are represented as variants, special routines are required that convert a variant value to a calendar date format and backwards. These operations are performed by the SetDateElements, GetYear, GetMonth and GetDay methods of the object. These methods take into account whether the current year is a leap year and the number of days in a month to guarantee that the value is valid. accepts the parts of a date and returns a variant date value.

The , and methods perform a contrary operation: they accept a variant date/time value and return respective date parts of that value. Here is an example of how to use these routines:

VBScript

Sub EncodeDateDemo

' Create a Date variable having the specified year, month and day values
  myDate=aqDateTime.SetDateElements(2005,12,25)

' Convert the value of the myDate variable to a string using the specified format and post this string to the log
  EncodedDate = aqConvert.DateTimeToFormatStr(myDate,"%B/%#d/%Y")
  Log.Message("The encoded date is "+ EncodedDate)

' Convert the value of the myDate variable to a variant value and post it to the log
  VariantDate = aqConvert.IntToStr(myDate)
  Log.Message("The variant representation of it is "+ VariantDate)
EndSub

VBScript

Sub DecodeDateDemo
Dim Year, Month, Day

' Obtain the current date
  CurrentDate=aqDateTime.Today

' Return the parts of the current date value and then post them to the log
  Year=aqDateTime.GetYear(CurrentDate)
  Month=aqDateTime.GetMonth(CurrentDate)
  Day=aqDateTime.GetDay(CurrentDate)

  Log.Message(aqConvert.IntToStr(Day)+" day(s) have passed since the beginning of the "+aqConvert.IntToStr(Month)+" month of "+aqConvert.IntToStr(Year)+" year.")
EndSub

Modifying Date Values

Despite the fact that date/time values are represented as floating-point numbers, you cannot explicitly use ordinary arithmetic operators to change dates or time. However, the scripting object provides a number of methods that were designed to modify date/time values. These methods are: , and . The method allows you to modify date and time portions at once.

As it is said above, these methods are used both for incrementing and decrementing date/time values. If a positive number is passed as Month, Days or as another parameter, the resulting value is increased. If the parameter is negative, the resulting value is decreased.

These methods take into account the number of days in the month, whether the year is a leap year and other aspects of time calculations, that is why the resulting value is guaranteed to be valid.

The code below demonstrates how to use these methods:

VBScript

Dealing with Week Days

Besides recognizing the date, month and year, you may need to know which day of the week a certain date is. The method can help you do this. It accepts a variant date and returns the number of the week day that corresponds to it. The returned number ranges from 1 to 7, where 1 corresponds to Sunday, 2 - to Monday, and so on. The sample code below obtains the current date, calculates the day of the week and posts the name of the day to the log.

Note: The method uses the United States system of week notation where a week starts on Sunday and ends on Saturday. However, the ISO 8601 “Data elements and interchange formats - Information interchange - Representation of dates and times” standard recommends another system of week notation. In this system, a week starts on Monday and ends on Sunday. To convert a US week day number to the ISO 8601 format, you can use the following routine: The result of this function is also an integer number, but 1 corresponds to Monday, 2 to Tuesday and so on.

VBScript

Function ISODayOfWeek(InputDate)
  ISODayOfWeek=aqDateTime.GetDayOfWeek(InputDate)-1
If (ISODayOfWeek=0) Then ISODayOfWeek=7
EndFunction

In addition to the day of the week, we can find out the week boundaries that the specified date belongs, that is, calculate the dates that the week starts and ends. This could be done using the following two routines. Here the number of a week day is subtracted from the specified date to get the beginning of a week, and to get the end of the week, six days are added to the calculated date.

Note: Since the routines use the method, which returns a US week day number, the dates returned by the routines are Sunday and Saturday, correspondingly. To apply the ISO 8601 week day notation, you should use the last code line where the call is replaced with a call to the function (the code of the function is provided in the note above). In this case, the first routine will return the date that falls on Monday and the second one will return the date that falls on Sunday.

Another frequent operation when dealing with dates is calculating the number of weeks that have passed since the beginning of the year. First we should clarify what week should be considered as the first week of a year. According to the ISO 8601 standard, the first week of a year is a week with the majority (four or more) of days in the starting year. In our case it is better to use another equivalent definition of the first week: the week with the date, January, 4th.

Here is the routine that calculates the week number in compliance with this rule. It accepts the date that belongs to the desired week, determines the end of this week and the end of the week that includes January 4, and that calculates the week number as the difference between those two values divided by 7.

Sometimes it is useful to know what day of the week the desired month starts or ends. The routines below return the week day number that correspond to the beginning and end of the month. They only have one input parameter that specifies the date which belongs to the desired month. If the parameter is missing then the routines return the results for the current month. The routines are similar to calculating the beginning and end of a week, but instead of the week day number, the day of the month number is used.

VBScript

Sub DayOfWeekDemo
Dim WeekDay, DayName
  WeekDay=aqDateTime.GetDayOfWeek(aqDateTime.Today)
SelectCase WeekDay
Case 1: DayName="Sunday"
Case 2: DayName="Monday"
Case 3: DayName="Tuesday"
Case 4: DayName="Wednesday"
Case 5: DayName="Thursday"
Case 6: DayName="Friday"
Case 7: DayName="Saturday"
EndSelect
  Log.Message("Today is "+DayName)
EndSub

VBScript

VBScript

Function WeekNumber(InputDate)
' Truncate the input parameter
  InputDate=Fix(InputDate)
  YearNo=aqDateTime.GetYear(InputDate)
  EndOfFirstWeek=EndOfWeek(aqDateTime.SetDateElements(YearNo,1,4))
  EndOfCurrentWeek=EndOfWeek(InputDate)
  WeekNumber= 1+(aqDateTime.GetDayOfYear(EndOfCurrentWeek)-aqDateTime.GetDayOfYear(EndOfFirstWeek))/7
EndFunction

VBScript

See Also

Working With Dates
VBScript - Working With Time Values
aqDateTime Object