R — Dates
Basic dates
# Current date
today <- Sys.Date()
print(today)
# Create date
date1 <- as.Date("2026-08-23")
date2 <- as.Date("23/08/2026", format = "%d/%m/%Y")
# Difference
diff <- date2 - date1
print(diff)
lubridate
library(lubridate)
# Parse dates
ymd("2026-08-23")
mdy("08/23/2026")
dmy("23/08/2026")
# Current datetime
now()
# Components
year(today)
month(today)
day(today)
wday(today)
Date arithmetic
library(lubridate)
today <- today()
# Add/subtract
today + days(7)
today + months(1)
today + years(1)
# Difference
difftime(today + days(30), today, units = "days")
Formatting
library(lubridate)
now <- now()
format(now, "%Y-%m-%d %H:%M:%S")
format(now, "%B %d, %Y")
format(now, "%A, %B %d, %Y")
Mini Practice
Write R code that:
- Creates and parses dates
- Extracts date components
- Performs date arithmetic
- Formats dates for display
Up Next
In the next lesson, you'll learn about Packages — extending R with packages.
Related Topics
Frequently Asked Questions about Dates
What is Dates in R?
Dates is a fundamental concept in R. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Dates?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Dates.
Why is Dates important in R?
Dates is essential for R development. Understanding this concept will help you write better code and solve real-world problems more effectively.