Monday, January 26, 2009

22 VBScript Date & Time Functions with Examples

All of the output values are shown in green.
All Date & Time functions are in bold.

1.
MyDate = Date
msgbox MyDate
Shows 1/12/2009 in mm/dd/yyyy format

2.
MyVar = Now
msgbox MyVar
Shows 1/12/2009 5:28:48 PM

3.
MyTime = Time
msgbox MyTime
Shows 5:28:48 PM

4.
MyDay = Day ("January 12, 2009")
msgbox MyDay
Shows 12

5.
MyTime = Now
MyHour = Hour (MyTime)
msgbox MyHour
Shows 17 at 5'o clock

6.
MyVar = Minute (Now)
msgbox MyVar
Shows 34 at 5:34 PM

7.
MySec = Second (Now)
msgbox MySec
Shows 51 at 5:34:51 PM

8.
MyDate = #January 12, 2009# ' Assign a date.
MyWeekDay = Weekday (MyDate)
msgbox MyWeekDay
1 for Sunday, 2 for Monday... so on

9.
MyDate = #January 12, 2009# ' Assign a date.
MyYear = Year (MyDate)
msgbox MyYear
Shows 2009

10.
MyVar = Month (Now)
msgbox MyVar
Shows 1 for January and so on.

11.
IsDate Function
IsDate returns True if the expression is a date or can be converted to a valid date; otherwise, it returns False.

MyDate = "January 12, 2009": YourDate = #01/12/09#: NoDate = "Yes Boss"
MyCheck = IsDate (MyDate) ' Returns True.
MyCheck = IsDate (YourDate) ' Returns True.
MyCheck = IsDate (NoDate) ' Returns False.

12.
CDate Function

The CDate function converts a valid date and time expression to type Date.

Use the IsDate function to determine if date can be converted to a date or time. The IsDate function uses local setting to determine if a string can be converted to a date.


The following example uses the CDate function to convert a string to a date.

x="January 12, 2009"
if IsDate (x) then
msgbox (CDate (x))
end if

Output: 1/12/2009

13.
DateAdd(interval, number, date)

It returns a date after adding a specified time interval.

Msgbox (DateAdd("m",1,"31-Mar-09"))
Shows 4/30/2009

Msgbox (DateAdd("m",-1,"31-Mar-09"))
Shows 2/28/2009

Interval can be YYYY - Year, q - Quarter, m - Month, h - Hour, n - Minute etc.

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

The DateDiff function returns the number of intervals between two dates.

Msgbox(DateDiff("m",Date,"12/31/2009"))
or
Msgbox(DateDiff("m","03/14/2009","12/31/2009"))
Shows 9

Interval can be YYYY - Year, q - Quarter, m - Month, h - Hour, n - Minute etc.

firstdayofweek (optional) has values like 0 for vbUseSystemDayOfWeek, 1 for vbSunday, 2 for vbMonday and so on.

firstweekofyear (optional) 0 for vbUseSystem, 1 for vbFirstJan1 i.e. Start with the week in which January 1 occurs (default), 2 for vbFirstFourDays i.e. Start with the week that has at least four days in the new year, 3 for vbFirstFullWeek i.e. Start with the first full week of the new year

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

DatePart function returns the specified part of a given date.

Msgbox(DatePart("d", Date))
or
Msgbox(DatePart("d","3/14/2009"))
Shows 14

Interval can be YYYY - Year, q - Quarter, m - Month, h - Hour, n - Minute etc.

firstdayofweek (optional) has values like 0 for vbUseSystemDayOfWeek, 1 for vbSunday, 2 for vbMonday and so on.

firstweekofyear (optional) 0 for vbUseSystem, 1 for vbFirstJan1 i.e. Start with the week in which January 1 occurs (default), 2 for vbFirstFourDays i.e. Start with the week that has at least four days in the new year, 3 for vbFirstFullWeek i.e. Start with the first full week of the new year.

16.
DateSerial returns a Date value representing a specified year, month, and day, with the time information set to midnight (00:00:00)

DateSerial(year, month, day)

Msgbox(DateSerial(2009,11,29))
Shows 11/29/2009

Msgbox(DateSerial(2009-9,9-2,1-1))
Shows 6/30/2000

17.
The DateValue function returns a type Date.

DateValue(date)

Msgbox(DateValue("31-Jan-09"))
Shows 1/31/2009

18.
The MonthName function returns a string indicating the specified month.

MonthName(month[, abbreviate])

Msgbox(MonthName(9))
Shows September

month = Required. Specifies the number of the month (January is 1, February is 2, etc.)

abbreviate = Optional. A Boolean value that indicates if the month name is to be abbreviated. Default is False

19.
Timer
The Timer function returns the number of seconds that have passed since midnight (12:00:00 AM).

Msgbox(Timer)
Shows 86296.13 at 11:58:16 PM

20.
TimeSerial
The TimeSerial function returns the time for a specific hour, minute, and second.

TimeSerial(hour, minute, second)

Msgbox(TimeSerial(0,18,11))
Shows 12:18:11 AM

21.
The TimeValue function converts an argument into the variant of subtype Date.

TimeValue(time)

Msgbox(TimeValue("3:23:59 PM"))
Shows 3:23:59 PM

22.
WeekdayName returns a string indicating the specified day of the week.

WeekdayName(weekday[,abbreviate[,firstdayofweek]])

weekday Required. The number of the weekday

abbreviate Optional. A Boolean value that indicates if the weekday name is to be abbreviated

firstdayofweek Optional. Specifies the first day of the week.
firstdayofweek (optional) has values like 0 for vbUseSystemDayOfWeek, 1 for vbSunday, 2 for vbMonday and so on.


Msgbox(WeekdayName(4))
Shows Wednesday

Msgbox(WeekdayName(Weekday(Date)))
Shows Sunday (obviously if today is Sunday)