Vba datetime function in Excel
The Date and Time function in VBA plays a key role in applications related to scheduling and Attendance Monitoring.
Any Excel sheet with data, will have a date column associated with it and a method to work with it is inevitable.
The Date and Time function in VBA offers a number of methods which are discussed as below.
Broadly, There are three things associated with this feature.They are
The 'Date' returns the Date portion of the current date.It can be converted to any format using the format function.
The 'Time' returns theTime part of the current date and time, which can also be formated accordingly.
The 'Now' returns the whole Date and Time.
These functions are very useful while programming a complex application in VBA.
For example, If we want a code to execute on a particular day or after a particular time,
then we can use this Date and Time function to handle these requirements.
Also, there are other features associated with these functions.
For instance, The Date can be used to return day,month and year part alone.
Also, the Time part can be used to retrive Hour, Minute or the Seconds part.
The following example illustrates this
- Sub date_time()
- MsgBox "Current Date : " & Date
- MsgBox "Current Time : " & Time
- MsgBox "Current Date and Time : " & DateTime.Now
- MsgBox "Current Day " & Day(Date)
- MsgBox "Current Month " & Month(Date)
- MsgBox "Current Year : " & Year(Date)
- MsgBox "Hour part : " & Hour(Time)
- MsgBox "Minute part : " & Minute(Time)
- End Sub