You are on page 1of 20

Suresh4qtp#gmail.

com

VBScript Date & Time Functions with Examples


All of the output values are shown in green. All Date & Time functions are in bold.
Date and Time Constants
The interval argument can have the following values:
Setting
yyyy
Q
M
Y
D
W
ww
H
N
S

Description
Year
Quarter
Month
Day of year
Day
Weekday
Week of year
Hour
Minute
Second

The firstdayofweek argument can have the following values:


Constant
vbUseSystemDayOfWeek
vbSunday
vbMonday
vbTuesday
vbWednesday
vbThursday
vbFriday
vbSaturday

Value
0
1
2
3
4
5
6
7

Description
Use National Language Support (NLS) API setting.
Sunday (default)
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

The firstweekofyear argument can have the following values:


Constant
vbUseSystem
vbFirstJan1
vbFirstFourDays
vbFirstFullWeek

Value
0
1
2
3

Description
Use National Language Support (NLS) API setting.
Start with the week in which January 1 occurs (default).
Start with the week that has at least four days in the new year.
Start with the first full week of the new year.

Month Constants

Constant

Month

January

2
3
4
5

February
March
April
May

Suresh4qtp#gmail.com

6
7
8

June
July
August

September

10
11
12

October
November
December

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

Now
MyVar = Now
Msgbox MyVar
Shows 1/12/2009 5:28:48 PM

Time
MyTime = Time
Msgbox MyTime
Shows 5:28:48 PM

Day
MyDay = Day ("January 12, 2009")
Msgbox MyDay
Shows 12

Suresh4qtp#gmail.com

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

Minute
MyVar = Minute (Now)
Msgbox MyVar
Shows 34 at 5:34 PM

Second
MySec = Second (Now)
Msgbox MySec
Shows 51 at 5:34:51 PM

WeekDay
MyDate = #January 12, 2009# ' Assign a date.
MyWeekDay = Weekday (MyDate)
Msgbox MyWeekDay
Shows 2

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

Suresh4qtp#gmail.com

Shows 2009

Month
MyVar = Month (Now)
Msgbox MyVar
Shows 1

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

In Microsoft Windows, the range of valid dates is January 1, 100 A.D. through
December 31, 9999 A.D.; the ranges vary among operating systems.

Question: What is the meaning of BC and AD (B.C. and A.D.)?


Answer: It is commonly thought that BC stands for "before Christ" and AD stands
for "after death".

MyDate = "January 12, 2009"


YourDate = #01/12/09#
NoDate = "Yes Boss"
MyCheck = IsDate (MyDate)
Msgbox MyCheck
Shows True.

MyCheck = IsDate (YourDate)


Msgbox MyCheck
Shows True.

Suresh4qtp#gmail.com

MyCheck = IsDate (NoDate)


Msgbox MyCheck
Shows False.

CDate
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
Shows 1/12/2009.

DateAdd
DateAdd function returns a date after adding a specified time interval.
Syntax: 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.

Suresh4qtp#gmail.com

Remarks:

You can use the DateAdd function to add or subtract a specified time interval from a
date. For example, you can use DateAdd to calculate a date 30 days from today or a
time 45 minutes from now. To add days to date, you can use Day of Year ("y"), Day
("d"), or Weekday ("w").

Msgbox (DateAdd("m", 1, "31-Mar-09"))


Shows 4/30/2009.

Msgbox (DateAdd("m", -1, "31-Mar-09"))


Shows 2/28/2009.

Msgbox (DateAdd("m", 1, "31-Jan-95"))


Shows 2/28/1995.

Msgbox (DateAdd("m", 1, "31-Jan-96"))


Shows 2/29/1996. because 1996 is a leap year.

DateDiff
DateDiff function returns the number of intervals between two dates.
Syntax: 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 Interval Argument
Table 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

Suresh4qtp#gmail.com

specified, Sunday is assumed. See Date and Time Constants 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 Interval Argument Table 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.
Msgbox(DateDiff("m", "03/14/2009", "12/31/2009"))
Shows 9.

To calculate the number of days between date1 and date2, you can use either
Day of year ("y") or Day ("d").

Msgbox(DateDiff("d", "06/14/2011", "06/14/2012"))


Shows 366.
Or
Msgbox(DateDiff("y", "06/14/2011", "06/14/2012"))
Shows 366.

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.

Msgbox(DateDiff("w", "06/12/2012", "06/16/2012"))


Shows 0. Here there is no Mondays between two dates
Msgbox(DateDiff("w", "06/04/2012", "06/11/2012"))
Shows 1. Here Mondays between two dates 4, 11

Suresh4qtp#gmail.com

Msgbox(DateDiff("w", "06/04/2012", "06/18/2012"))


Shows 2. Here Mondays between two dates 4, 11, 18

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.

Msgbox(DateDiff("ww", "06/04/2012", "06/09/2012"))


Shows 0. Here there is no Sundays Between two dates

Msgbox(DateDiff("ww", "06/03/2012", "06/17/2012"))


Shows 2. Here Sundays Between two dates 3, 10, 17 but shows 2
Msgbox(DateDiff("ww", "05/30/2012", "06/10/2012"))
Shows 2. Here Sundays Between two dates 3, 10

Msgbox(DateDiff("ww", "05/26/2012", "06/10/2012"))


Shows 3. Here Sundays Between two dates 27, 3, 10

Msgbox(DateDiff("ww", "06/01/2012", "06/10/2012"))


Shows 2. Here Sundays Between two dates 3, 10

Msgbox(DateDiff("ww", "06/01/2012", "06/17/2012"))


Shows 3. Here Sundays Between two dates 3, 10, 17

If date1 refers to a later point in time than date2, the DateDiff function returns a
negative number.

Msgbox(DateDiff("ww", "06/24/2012", "06/17/2012"))

Suresh4qtp#gmail.com

Shows -1. Here Sundays Between two dates 17, 24 but it shows only 1

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.

Msgbox(DateDiff("yyyy", "December 31, 2011", "January 1" ))


Shows 1. Here gap is only one day but it shows gap 1 year.

Msgbox(DateDiff("yyyy", "December 31", "January 1" ))


Shows 0, why because it takes current year.

DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.

Msgbox(DateDiff("yyyy", "December 31, 2012", "January 1, 2013"))


Shows 1. Here gap is only one day but it shows gap 1 year.

DatePart
DatePart function returns the specified part of a given date.
Syntax: DatePart(interval, date[,firstdayofweek[,firstweekofyear]])

Msgbox(DatePart("d", Date))
Or
Msgbox(DatePart("m", "3/14/2009"))
Shows 3.

Suresh4qtp#gmail.com

DateSerial
DateSerial function returns a Date value representing a specified year, month, and
day, with the time information set to midnight (00:00:00)
Syntax: DateSerial(year, month, day)
Arguments
Year: Required. Number between 0 and 9999, inclusive.
Month: Required. Any numeric expression to express the month ( 1-12).
Day: Required. Any numeric expression to express the day (1-31).

Return Value :

A Date.

If the value of a particular element exceeds its normal limits, DateSerial adjusts
the date accordingly. For example, if you tried DateSerial (96,2,31) means
February 31, 1996 - DateSerial returns March 2, 1996.

Msgbox(DateSerial (96, 2, 31))


Shows 3/2/1996.

You can specify expressions or formulas that evaluate to individual date components
as parameters to DateSerial. For example, DateSerial (98,10+9,23) returns 23 March
1999. This makes it easier to use DateSerial to form dates whose individual elements
are unknown at design time or that are created on the fly because of user input.

Msgbox(DateSerial (98, 10+9, 23))


Shows 7/23/1999.

Msgbox(DateSerial(2009, 11, 29))


Shows 11/29/2009.

10

Suresh4qtp#gmail.com

Msgbox(DateSerial(2009-9, 9-2, 1-1))


Shows 6/30/2000.

Msgbox(DateSerial(1990 - 10, 8 - 2, 1 - 1))


Shows 5/31/1980.

For the year argument, values between 0 and 99, inclusive, are interpreted as
the years 20002999. For all other year arguments, use a complete four-digit
year (for example, 1800).

Msgbox(DateSerial(0, 8, 1))
Shows 8/1/2000.

Msgbox(DateSerial(99, 8, 1))
Shows 8/1/1999.

Msgbox(DateSerial(0, 8, 1))
Shows 8/1/2000.

Msgbox(DateSerial(1999, 8, 1))
Shows 8/1/1999.

Msgbox(DateSerial(1000, 8, 1))
Shows 8/1/1000.

Msgbox(DateSerial(100, 8, 1))
Shows 8/1/100.

11

Suresh4qtp#gmail.com

DateValue
DateValue function returns a type Date.
Syntax: DateValue(date)

If the date argument includes time information, DateValue doesn't return it.

Msgbox(DateValue("June 16, 2012 12:29 AM"))


Shows 6/16/2012.

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

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

However, if date includes invalid time information (such as "89:98"), an error


occurs.

Msgbox(DateValue("June 16, 2012 89:98"))


Shows Syntax Error.

MonthName
MonthName function returns a string indicating the specified month.
Syntax: MonthName(month[, abbreviate])
Arguments
Month: Required. The numeric designation of the month. For example, January is 1,
February is 2, and so on.
Abbreviate: Optional. Boolean value that indicates if the month name is to be

12

Suresh4qtp#gmail.com

abbreviated. If omitted, the default is False, which means that the


month name is not abbreviated.

Msgbox(MonthName(9))
Shows September.

Msgbox(MonthName(9, False))
Shows September.

Msgbox(MonthName(9, True))
Shows September.

Timer
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

TimeSerial
TimeSerial function returns the time for a specific hour, minute, and second.
Syntax: TimeSerial(hour, minute, second)
Arguments
Hour: Number between 0 (12:00 A.M.) and 23 (11:00 P.M.), inclusive, or a numeric
expression.
Minute: Any numeric expression.
Second: Any numeric expression.

13

Suresh4qtp#gmail.com

Msgbox(TimeSerial(0, 18, 11))


Shows 12:18:11 AM

TimeValue
TimeValue function converts an argument into the variant of subtype Date.
Syntax: TimeValue(time)
The time argument is usually a string expression representing a time from 0:00:00
(12:00:00 A.M.) to 23:59:59 (11:59:59 P.M.), inclusive. However, time can also be
any expression that represents a time in that range.
Msgbox(TimeValue("3:23:59 PM"))
Shows 3:23:59 PM

Msgbox(TimeValue("3:23"))
Shows 3:23:00 AM

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

Msgbox(TimeValue("00:00:00"))
Shows 12:00:00 AM

Msgbox(TimeValue("June 15, 2012 5:10:20"))


Shows 05:10:20 AM

Msgbox(TimeValue("June 15, 2012 5:10:20 PM"))


Shows 05:10:20 PM

14

Suresh4qtp#gmail.com

Msgbox(TimeValue(#5:10:20 PM#))
Shows 05:10:20 PM

WeekdayName
WeekdayName function returns a string indicating the specified day of the week.
Syntax: WeekdayName(weekday[,abbreviate[,firstdayofweek]])
Arguments
Weekday: Required. The numeric designation for the day of the week. Numeric
value of each day depends on setting of the firstdayofweek setting.
Abbreviate: Optional. Boolean value that indicates if the weekday name is to be
abbreviated. If omitted, the default is False, which means that the
weekday name is not abbreviated.
Firstdayofweek: Optional. Numeric value indicating the first day of the week
values like 0 for vbUseSystemDayOfWeek, 1 for vbSunday,
2 for vbMonday and so on.

Msgbox(WeekdayName(4))
Shows Wednesday.

Msgbox(WeekdayName(4, True))
Shows Wed.

Msgbox(WeekDayName(2, True))
Shows Mon.

Msgbox(WeekDayName(2, True, 4))


Shows Thu.

15

Suresh4qtp#gmail.com

Msgbox(WeekdayName(Weekday(Date)))
Shows today name.

FormatDateTime
FormateDateTime function Returns an expression formatted as a date or time.
Syntax : FormatDateTime(Date[, NamedFormat])
Arguments
Date: Required. Date expression to be formatted.
NamedFormat: Optional. Numeric value that indicates the date/time format used. If
omitted, vbGeneralDate is used.
The NamedFormat argument has the following settings:
Constant

Value

vbGeneralDate

vbLongDate

vbShortDate

vbLongTime

vbShortTime

Description
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.
Display a date using the long date format specified in your
computer's regional settings.
Display a date using the short date format specified in your
computer's regional settings.
Display a time using the time format specified in your computer's
regional settings.
Display a time using the 24-hour format (hh:mm).

Msgbox(FormatDateTime("March 02, 1986", vbGeneralDate))


Shows March 02, 1986.

Msgbox(FormatDateTime("March 02, 1986", vbLongDate))


Shows Sunday, March 02, 1986.

Msgbox(FormatDateTime("March 02, 1986", vbShortDate))


Shows 3/2/1986.

16

Suresh4qtp#gmail.com

Msgbox(FormatDateTime(Now, vbLongTime))
Shows 6:46:44 PM. It displays the current execution time.

Msgbox(FormatDateTime(Now, vbShortTime))
Shows 18:46. It displays the current execution time.

Programs
1. Write a program to find a number of days up to Current Date.
Dim dBirthDay
Dim strQuestion
Dim strM
strQuestion = "Enter the Date you were born : "
dBirthDay = InputBox(strQuestion& vbLf & vbLf &"mm/dd/yyyy", "DateDiff Example")
If IsDate(dBirthDay) Then
strM = "If you're born on " & FormatDateTime(dBirthDay, vbLongDate)
strM = strM & " then you're now ..." & vbCrLf & vbCrLf
strM = strM & "... " & DateDiff("D", dBirthDay, Date) & " days old !"
MsgBox strM, vbInformation, "DateDiff Example"
Else
strM = "You did not enter a correct date!"
MsgBox strM, vbExclamation, "DateDiff Example"
End If

17

Suresh4qtp#gmail.com

2. I want this format dd.mm.yyyy (26.06/2012) but Date function shows


mm/dd/yyyy format (6/26/2012).
Msgbox DotNetFactory.CreateInstance("System.DateTime").Now.ToString("dd.MM.yyyy")
OR
Msgbox Right("0"&Day(Now), 2)&"."&Right("0"&Month(Now), 2)&"."&Year(Now)

3. Calculate the Years, Months, Days, Hours, Minutes and Seconds From given two
dates?
Function datediffToWords(d1, d2)
Dim Report, Days, Months, Years, Seconds
Dim NewStart, FullMonthStart
Report = ""
'Start with total number of days
Days = DateDiff("d", d1, d2)
'Convert days to years and grab remaining days
If Days > 365 Then
Years = Days\365
Days = Days Mod (365 * Years) - 1
Report = Years & " Year(s), "
Else
Years = 0
End If
'Compute the number of months
Months = Int(DateDiff("m", d1, d2)) + (Day(d2)<Day(d1))
'Remove years from the total months
Months = Months Mod 12
Report = Report & Months & " Month(s), "

18

Suresh4qtp#gmail.com

'Now find the days

NewStart = Month(d1) & "/" & Day(d1) & "/" & Year(d1) + Years
If Month(d1) <> 12 Then
FullMonthStart = Month(d1) + 1 & "/1/" & Year(d1) + Years
Else
FullMonthStart = "1/1/" & Year(d1) + Years + 1
End If
If Day(d1) =< Day(d2) Then
Days = Day(d2) - Day(d1)
Else
Days = DateDiff("d", NewStart, FullMonthStart) + Day(d2) - 1
End If
If Days > 0 Then
Report = Report & Days & " day(s), "
End If
'Now we will deal with the time left over
'Begin by getting total seconds between dates and divide out the days
'Grab the remaining seconds with the mod operator
Seconds = Abs(DateDiff("S", d1, d2))
If Seconds <= 0 Then
Report = "0 seconds."
Else
Seconds = Seconds Mod (24*60*60)
'Divide by 3600 to get hours
If Seconds >= 3600 Then
Report = Report & Seconds\(3600) & " hours(s), "
End If

19

Suresh4qtp#gmail.com

'Use mod to get remaining seconds and divide to get minutes


Seconds = Seconds Mod (60*60)
If Seconds >= 60 Then
Report = Report & Seconds\(60) & " minutes(s), "
End If
'Use mod to get remaining seconds
Seconds = Seconds Mod (60)
Report = Report & Seconds & " second(s)"
End if
datediffToWords = Report
End Function

' Function Call


sTime = "6/30/2002 4:15:00 PM"
eTime = "9/28/2007 5:25:50 PM"
Msgbox datediffToWords(sTime, eTime)
OutPut

20

You might also like