VBA Date and Time Function
VBScript Date and Time Functions help the developers to convert date and time from one format to another or to express the date or time value in the format that suits a specific condition.
Date Function
The Function returns the current system date.
Syntax:-
date()
Example:-
Private Sub Constant_demo_Click()
Dim a as Variant
a = date()
msgbox "The Value of a : " & a
End Sub
Dim a as Variant
a = date()
msgbox "The Value of a : " & a
End Sub
CDate Function
The Function converts a valid date and time expression to type date.
Syntax:-
cdate(date)
Example:-
Private Sub Constant_demo_Click()
Dim a as Variant
Dim b as Variant
a = cdate("Jan 01 2020")
msgbox("The Value of a : " & a)
b = cdate("31 Dec 2050")
msgbox("The Value of b : " & b)
End Sub
Dim a as Variant
Dim b as Variant
a = cdate("Jan 01 2020")
msgbox("The Value of a : " & a)
b = cdate("31 Dec 2050")
msgbox("The Value of b : " & b)
End Sub
DateAdd Function
A Function, which returns a date to which a specified time interval has been added.
Syntax:-
DateAdd(interval,number,date)
Parameter Description
- Interval:- A required parameter. It can take the following values.
- d - day of the year
- m - month of the year
- y - year of the year
- yyyy - year
- w - weekday
- ww - week
- q - quarter
- h - hour
- m - minute
- s - second
- Number:-A required parameter. It can take both positive and negative parameters.
- Date:-A required parameter. A variant or literal representing the date to which an interval is added.
Example:-
msgbox("Line 1 : " &DateAdd("yyyy",-1,date1))
DateDiff Function
A Function, which returns the difference between two specified time intervals.
Syntax:-
DateDiff(interval, date1, date2)
Example:-
Private Sub Constant_demo_Click()
Dim fromDate as Variant
fromDate = "01-Jan-09 00:00:00"
Dim toDate as Variant
toDate = "01-Jan-10 23:59:00"
msgbox("Line 1 : " &DateDiff("yyyy",fromDate,toDate))
End Sub
Dim fromDate as Variant
fromDate = "01-Jan-09 00:00:00"
Dim toDate as Variant
toDate = "01-Jan-10 23:59:00"
msgbox("Line 1 : " &DateDiff("yyyy",fromDate,toDate))
End Sub
DatePart Function
A Function, which returns the specific part of the given date.
Syntax:-
DatePart(interval,date)
Example:-
Private Sub Constant_demo_Click()
Dim Quarter as Variant
Dim DayOfYear as Variant
Dim WeekOfYear as Variant
Date1 = "2013-01-15"
Quarter = DatePart("q", Date1)
msgbox("Line 1 : " & Quarter)
End Sub
Dim Quarter as Variant
Dim DayOfYear as Variant
Dim WeekOfYear as Variant
Date1 = "2013-01-15"
Quarter = DatePart("q", Date1)
msgbox("Line 1 : " & Quarter)
End Sub
DateSerial Function
A Function, which returns a date for the specified day, month, and year parameters.
Syntax:-
DateSerial(year,month,day)
Example:-
Private Sub Constant_demo_Click()
msgbox(DateSerial(2013,5,10))
End Sub
msgbox(DateSerial(2013,5,10))
End Sub
Format DateTime Function
A Function, which helps the developers to format and return a valid date and time expression.
Syntax:-
FormatDateTime(date,format)
Example:-
Private Sub Constant_demo_Click()
msgbox("Line 2 : " & FormatDateTime(d,1))
End Sub
msgbox("Line 2 : " & FormatDateTime(d,1))
End Sub
IsDate Function
A Function, which returns a Boolean value whether or not the given input is a date.
Syntax:-
IsDate(expression)
Example:-
Private Sub Constant_demo_Click()
msgbox("Line 1 : " & IsDate("Nov 03, 1950"))
End Sub
msgbox("Line 1 : " & IsDate("Nov 03, 1950"))
End Sub
Day Function
The Day function returns a number between 1 and 31 that represents the day of the specified date.
Syntax:-
Day(date)
Example:-
Private Sub Constant_demo_Click()
msgbox(Day("2013-06-30"))
End Sub
msgbox(Day("2013-06-30"))
End Sub
Month Function
The Month function returns a number between 1 and 12 that represents the month of the specified date.
Syntax:-
Month(date)
Example:-
Private Sub Constant_demo_Click()
msgbox(Month("2013-06-30"))
End Sub
msgbox(Month("2013-06-30"))
End Sub
Year Function
The Year function returns an integer that represents a year of the specified date.
Syntax:-
Year(date)
Example:-
Private Sub Constant_demo_Click()
msgbox(Year("2013-06-30"))
End Sub
msgbox(Year("2013-06-30"))
End Sub
Month Name
The MonthName function returns the name of the month for the specified date.
Syntax:-
MonthName(month)
Example:-
Private Sub Constant_demo_Click()
msgbox("Line 1 : " & MonthName(01,True))
End Sub
msgbox("Line 1 : " & MonthName(01,True))
End Sub
WeekDay
The WeekDay function returns an integer from 1 to 7 that represents the day of the week for the specified date.
Syntax:-
Weekday(date)
Example:-
Private Sub Constant_demo_Click()
msgbox("Line 1: " & Weekday("2013-05-16",1))
End Sub
msgbox("Line 1: " & Weekday("2013-05-16",1))
End Sub
WeekDay Name
The WeekDayName function returns the name of the weekday for the specified day.
Syntax:-
WeekdayName(weekday)
Example:-
Private Sub Constant_demo_Click()
msgbox("Line 1 : " &WeekdayName(3))
End Sub
msgbox("Line 1 : " &WeekdayName(3))
End Sub
Time Function
Now Function
The Function Now returns the current system date and time.
Syntax:-
Now()
Example:-
Private Sub Constant_demo_Click()
Dim a as Variant
a = Now()
msgbox("The Value of a : " & a)
End Sub
Dim a as Variant
a = Now()
msgbox("The Value of a : " & a)
End Sub
Hour Function
The Hour Function returns a number between 0 and 23 that represents the hour of the day for the specified time stamp.
Syntax:-
Hour(time)
Example:-
Private Sub Constant_demo_Click()
msgbox("Line 1: " & Hour("3:13:45 PM"))
msgbox("Line 2: " & Hour("23:13:45"))
msgbox("Line 3: " & Hour("2:20 PM"))
End Sub
msgbox("Line 1: " & Hour("3:13:45 PM"))
msgbox("Line 2: " & Hour("23:13:45"))
msgbox("Line 3: " & Hour("2:20 PM"))
End Sub
Minute Function
The Minute Function returns a number between 0 and 59 that represents the minute of the hour for the specified time stamp.
Syntax:-
Minute(time)
Example:-
Private Sub Constant_demo_Click()
msgbox("Line 1: " & Minute("3:13:45 PM"))
msgbox("Line 2: " & Minute("23:43:45"))
msgbox("Line 3: " & Minute("2:20 PM"))
End Sub
msgbox("Line 1: " & Minute("3:13:45 PM"))
msgbox("Line 2: " & Minute("23:43:45"))
msgbox("Line 3: " & Minute("2:20 PM"))
End Sub
Second Function
The Second Function returns a number between 0 and 59 that represents the second of the hour for the specified time stamp.
Syntax:-
Second(time)
Example:-
Private Sub Constant_demo_Click()
msgbox("Line 1: " & Second("3:13:25 PM"))
msgbox("Line 2: " & Second("23:13:45"))
msgbox("Line 3: " & Second("2:20 PM"))
End Sub
msgbox("Line 1: " & Second("3:13:25 PM"))
msgbox("Line 2: " & Second("23:13:45"))
msgbox("Line 3: " & Second("2:20 PM"))
End Sub
Time Function
The Time Function returns the current system time.
Syntax:-
Time()
Example:-
Private Sub Constant_demo_Click()
msgbox("Line 1: " & Time())
End Sub
msgbox("Line 1: " & Time())
End Sub
Timer Function
The Timer Function returns the number of seconds and milliseconds since 12:00 AM.
Syntax:-
Timer()
Example:-
Private Sub Constant_demo_Click()
msgbox("Time is : " & Now())
msgbox("Timer is: " & Timer())
End Sub
msgbox("Time is : " & Now())
msgbox("Timer is: " & Timer())
End Sub
Time Serial Function
The TimeSerial function returns the time for the specified hour, minute, and second values.
Syntax:-
TimeSerial(hour,minute,second)
Example:-
Private Sub Constant_demo_Click()
msgbox(TimeSerial(20,1,2))
msgbox(TimeSerial(0,59,59))
msgbox(TimeSerial(7*2,60/3,15+3))
End Sub
msgbox(TimeSerial(20,1,2))
msgbox(TimeSerial(0,59,59))
msgbox(TimeSerial(7*2,60/3,15+3))
End Sub
Time Value Function
The TimeValue Function converts the given input string to a valid time.
Syntax:-
TimeValue(StringTime)
Example:-
Private Sub Constant_demo_Click()
msgbox(TimeValue("20:30"))
msgbox(TimeValue("5:15"))
msgbox(TimeValue("2:30:58"))
End Sub
msgbox(TimeValue("20:30"))
msgbox(TimeValue("5:15"))
msgbox(TimeValue("2:30:58"))
End Sub