Date and Time in R with Lubridate

Last Updated : 15 Jul, 2025

Date and time in R represent temporal information and are handled using specific data types such as:

  • Date: Represents dates (e.g. "2025-07-15") without any time information.
  • POSIXct: Represents both date and time with a timestamp, stored as the number of seconds since January 1, 1970.
  • POSIXlt: Represents date and time as a list, with separate components for year, month, day, hour, minute, etc.

These different types in R are used to work with, compare and manipulate temporal data. Lubridate removes the complexity of dealing with these different date-time formats and makes it more simpler to work with time-related calculations and transformations.

Time Spans in R Using Lubridate

We will use the lubridate package to define and work with durations, intervals and periods between two specific datetime points in R programming language.

1. Creating Start and End Date-Time Objects

We create two datetime values in the US/Eastern timezone around a Daylight Saving Time transition.

  • mdy_hm: Parses date-time strings with month-day-year and hour-minute format.
  • tz: Assigns a timezone to the date-time object.
R
library(lubridate)

start <- mdy_hm("3-11-2017 5:21", tz = "US/Eastern")
end <- mdy_hm("3-12-2017 5:21", tz = "US/Eastern")

cat("Start time:", start, "\n")
cat("End time:", end, "\n")

Output:

Start time: 2017-03-11 05:21:00 EST

End time: 2017-03-12 05:21:00 EDT

2. Creating an Interval

We define the time span between the two datetime values using an interval.

  • %--%: Operator to create an interval between two dates.
  • Interval: Represents elapsed time with both start and end points.
R
time.interval <- start %--% end
cat("Interval:", time.interval, "\n")

Output:

Interval: 2017-03-11 05:21:00 EST--2017-03-12 05:21:00 EDT

3. Converting Interval to Duration

We convert the interval to a duration to get the exact elapsed time in seconds.

  • as.duration: Converts interval to duration object.
  • Duration: Measures the time span as a fixed number of seconds.
R
time.duration <- as.duration(time.interval)
cat("Duration:", time.duration, "\n")

Output:

Duration: 82800s (~23 hours)

4. Converting Interval to Period

We convert the interval to a period to express the time span in calendar units.

  • as.period: Converts interval into human-readable units (days, hours).
  • Period: Represents time in flexible calendar units, like days or months.
R
time.period <- as.period(time.interval)
cat("Period:", time.period, "\n")

Output:

Period: 1d 0H 0M 0S

Time Span Calculations and Conversions Using Lubridate

We perform conversions on time intervals and calculate durations in various units such as weeks, days, hours, minutes and years.

1. Creating and Converting Multiple Intervals

We define multiple start and end dates, then calculate elapsed time between them using intervals.

  • c(): Combines multiple date strings into a vector.
  • ymd(): Parses year-month-day formatted date strings.
  • %--%: Creates interval between start and end.
R
library(lubridate)

start <- c("2012-08-21", "2012-09-01", "2012-08-15", "2012-09-18")
end <- c("2012-09-16", "2012-09-06", "2012-08-22", "2012-10-11")

elapsed.time <- ymd(start) %--% ymd(end)
cat("Elapsed intervals:\n")
print(elapsed.time)

Output:

Elapsed intervals:

[1] 2012-08-21 UTC--2012-09-16 UTC 2012-09-01 UTC--2012-09-06 UTC

[3] 2012-08-15 UTC--2012-08-22 UTC 2012-09-18 UTC--2012-10-11 UTC

2. Converting Intervals to Different Units

We convert the intervals to durations and then divide by lubridate’s time unit functions.

  • dweeks(1): Returns duration of 1 week in seconds.
  • dhours(1): Returns duration of 1 hour in seconds.
  • ddays(1): Returns duration of 1 day in seconds.
  • dminutes(1): Returns duration of 1 minute in seconds.
  • dyears(1): Returns duration of 1 year in seconds.
R
cat("In weeks:", as.duration(elapsed.time) / dweeks(1), "\n")
cat("In hours:", as.duration(elapsed.time) / dhours(1), "\n")
cat("In days:", as.duration(elapsed.time) / ddays(1), "\n")
cat("In minutes:", as.duration(elapsed.time) / dminutes(1), "\n")
cat("In years:", as.duration(elapsed.time) / dyears(1), "\n")

Output:

date_time
Output

3. Calculating the Mean Duration in Weeks

We compute the average of the durations (in weeks).

  • mean: Computes the average of a numeric vector.
R
mean_weeks <- mean(as.duration(elapsed.time) / dweeks(1))
cat("Mean duration in weeks:", mean_weeks, "\n")

Output:

Mean duration in weeks: 2.178571

4. Converting Text Durations to Numeric Weeks

We convert character-based durations (like "12w 4d") to numeric weeks.

  • period: Converts character duration strings into period objects.
  • as.duration: Converts period to duration in seconds.
  • dweeks(1): Converts duration to weeks.
R
StudyTime <- c("12w 4d", "11w", "10w 5d", NA, "12w 6d")
weeks_numeric <- as.duration(period(StudyTime, units = c("week", "day"))) / dweeks(1)
cat("Text durations converted to numeric weeks:\n",weeks_numeric)

Output:

Text durations converted to numeric weeks:

12.57143 11 10.71429 NA 12.85714

Date Formatting Using Lubridate

We format dates from strings and extract or modify specific parts like year, month, day, hour, etc., using the lubridate package.

1. Creating and Formatting Dates

We convert string representations of dates and times into Date or POSIXct objects using various parsing functions.

  • ymd(): Parses strings in year-month-day format.
  • mdy_hm(): Parses strings in month-day-year with hour-minute format.
  • dmy_hms(): Parses strings in day-month-year with hour-minute-second format.
  • today(): Returns the current date.
  • now(): Returns the current date and time.
R
library(lubridate)

date1 <- ymd("2024-07-14")
date2 <- mdy_hm("07-14-2024 13:45")
date3 <- dmy_hms("14-07-2024 13:45:30")
current_date <- today()
current_datetime <- now()

cat("Parsed Date 1 (ymd):", date1, "\n")
cat("Parsed Date 2 (mdy_hm):", date2, "\n")
cat("Parsed Date 3 (dmy_hms):", date3, "\n")
cat("Current Date:", current_date, "\n")
cat("Current DateTime:", current_datetime, "\n")

Output:

time_date
Output

2. Extracting and Modifying Date Components

We extract or update parts of a date-time object such as year, month, weekday, hour, etc.

  • year(): Extracts the year.
  • month(): Extracts the month.
  • day(): Extracts the day.
  • wday(): Extracts the weekday; label = TRUE shows weekday name.
  • hour(): Extracts the hour.
  • minute(): Extracts the minute.
  • second(): Extracts the second.
  • year() <-: Updates the year.
  • month() <-: Updates the month.
R
cat("Year:", year(date3), "\n")
cat("Month:", month(date3), "\n")
cat("Day:", day(date3), "\n")
cat("Weekday:", wday(date3, label = TRUE), "\n")
cat("Hour:", hour(date3), "\n")
cat("Minute:", minute(date3), "\n")
cat("Second:", second(date3), "\n")

year(date3) <- 2025
month(date3) <- 12

cat("Updated Date3:", date3, "\n")

Output:

time
Output

The output displays individual date-time components and the updated date3 after changing its year and month.

Comment

Explore