Excel VBA provides various built-in date and time functions that allows us to write VBA codes involving dates and times. We can use date and time functions to display system date and time , add and substract data and time, converting string to date and more.
The date and time functions are explained in Table 11.1.
Function | Description |
---|---|
Now | returns current system date and time |
Date | returns current system date |
Day(Date) | Returns the day of the month for the date specified in the argument |
Weekday(Date) | Returns weekday as an integer for the date specified in the argument |
WeekdayName(Weekday(Date)) | Returns the name of weekday for the date specified in the argument |
WeekdayName(Weekday(Date),True) | Returns the abbrieviated name of weekday for the date specified in the argument |
Month(Date) | Returns the month of the year in integer for the date specified in the argument |
MonthName(Month(Date)) | Returns the name of month of the year for the date specified in the argument |
MonthName(Month(Date)) | Returns the abbrieviated name of month of the year for the date specified in the argument |
Year(Date) | Returns the year in integer for the date specified in the argument |
Private Sub CommandButton1_Click()
Cells(1, 2) = Now
Cells(2, 2) = Date
Cells(3, 2) = Day(Date)
Cells(4, 2) = Weekday(Date)
Cells(5, 2) = WeekdayName(Weekday(Date))
Cells(6, 2) = WeekdayName(Weekday(Date), "true")
Cells(7, 2) = Month(Date)
Cells(8, 2) = MonthName(Month(Date))
Cells(9, 2) = MonthName(Month(Date), "true")
Cells(10, 2) = Year(Date)
End Sub
The output is as shown in Figure 11.1
The time functions are explained in Table 11.2.
Function | Description |
---|---|
Time | Returns the current system time |
Hour | Returns the hour from its argument |
Minute | Returns the minute from its argument |
Second | Returns the second from its argument |
Timer | Returns the number of seconds since midnight |
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved [Privacy Policy]
Contact: Facebook Page