VBA – Date and Time
In VBA, dates and times are represented internally as a single double-precision floating-point number. The integer portion of this number represents the date, while the fractional portion represents the time. This standardized representation allows for easy manipulation and calculation of dates and times.
Date: Returns the current system date.
Time: Returns the current system time.
Now: Returns the current system date and time.
Timer: Returns the number of seconds since midnight.
DateSerial: Creates a date from specified year, month, and day.
TimeSerial(hour, minute, second): Creates a time from specified hour, minute, and second.
DateAdd(interval, number, date): Adds a specified interval (e.g., “yyyy” for year, “m” for month) to a date.
DateDiff(interval, date1, date2): Calculates the difference between two dates in a specified interval.
Weekday(date[, firstdayofweek]): Returns the day of the week for a given date (1 for Sunday to 7 for Saturday).
WeekdayName(weekday[, abbreviate]): Returns the name of the day of the week.
Month(date): Returns the month component of a date.
Day(date): Returns the day component of a date.
Year(date): Returns the year component of a date.
Hour(time): Returns the hour component of a time.
Minute(time): Returns the minute component of a time.
Second(time): Returns the second component of a time.
Format(date, format): Formats a date or time according to the specified format string.
IsDate(expression): Checks if an expression can be interpreted as a valid date.
This list provides a comprehensive overview of the primary date functions available in VBA. You can use these functions to perform various date-related operations within your VBA code, such as:
Calculating deadlines and due dates.
Determining the age of someone.
Analyzing date ranges.
Formatting dates for display.
Creating custom date-based reports.