Mastering Date Functions in VBA: A Comprehensive Guide

In this session, we dive into the various date functions available in VBA. The video covers essential functions like Date, CDate, DateAdd, DateDiff, DatePart, DateSerial, and others. Through practical examples in Visual Basic, you'll learn how to manipulate and format dates effectively.

What will you learn

  • How to use the Date function to get today's date.
  • Converting strings to dates using the CDate function.
  • Adding and subtracting dates with the DateAdd function.
  • Calculating differences between dates using the DateDiff function.
  • Extracting parts of a date with the DatePart function.
  • Constructing dates from individual components with the DateSerial function.
  • Formatting dates and times with the FormatDateTime function.
  • Checking if a given string is a date using IsDate.
  • Extracting day, month, and year from a date.
  • Getting the name of a month or day of the week using MonthName and WeekdayName.

Takeaway notes

  • The Date function returns the current date.
  • CDate function converts a string into a date format, ignoring the delimiters.
  • DateAdd can be used to add or subtract dates with precision down to seconds.
  • DateDiff calculates the difference between two dates and can return various units (years, months, days, etc.).
  • DatePart isolates specific parts of a date, like quarters or weeks.
  • DateSerial helps in constructing dates by combining year, month, and day.
  • FormatDateTime enables various formatting styles for date and time.
  • IsDate checks if the input is a valid date.
  • Functions like Day, Month, and Year extract respective parts of a date.
  • MonthName and WeekdayName provide the names of months and days with optional abbreviations.

Practice questions

  1. Use the Date function to print today's date in VBA.
  2. Convert the string "25-Dec-2021" into a date format using CDate and print it.
  3. Add 45 days to the current date using DateAdd and display the result.
  4. Subtract 3 months from "1-Jan-2020" using DateAdd and print the new date.
  5. Find the number of days between "1-Jan-2021" and "1-Jan-2022" using DateDiff.
  6. Extract the quarter from the date "15-Mar-2021" using DatePart.
  7. Create a date using DateSerial for the year 2021, month 12, and day 25.
  8. Format the date "15-Aug-2013" to a long date format using FormatDateTime.
  9. Check if the string "March 15, 2021" is a date using IsDate.
  10. Extract the day from the date "25-Dec-2021" using the Day function.
  11. Get the month name for the month number 5 and also display its abbreviation.
  12. Use Weekday to find out which day of the week "25-Dec-2021" falls on.
  13. Format the time "14:30" to a short time format using FormatDateTime.
  14. Write code to determine the difference in quarters between "1-Jan-2020" and "1-Jan-2022" using DateDiff.
  15. Display the full weekday name for a given date, e.g., "1-Jan-2021" using WeekdayName.

Mark Lesson Complete (Mastering Date Functions in VBA: A Comprehensive Guide)