Write R code to create a date variable birthday for "1990-05-22" and a datetime variable meeting for "2025-08-20 14:00:00". Then calculate the difference in days between "2025-08-20" and birthday.
R provides built-in classes to work with dates and times, such as Date and POSIXct/POSIXlt. These allow you to perform calculations, formatting, and comparisons easily.
my_date <- as.Date("2025-08-14")
my_date
my_datetime <- as.POSIXct("2025-08-14 15:30:00")
my_datetime
my_date + 5
difftime(as.Date("2025-08-20"), my_date)
format(my_date, "%B %d, %Y") # "August 14, 2025"
Working with dates and times is essential for time series analysis, scheduling, and reporting.