Base R has functions to get the current date and time.
The lubridate package offers fast and user-friendly parsing of date-time data.
# get date informationSys.Date()
## [1] "2025-10-02"# get current timeSys.time()
## [1] "2025-10-02 13:53:20 AEST"#install.packages("lubridate")library(lubridate)
# get current time using `lubridate`now()
## [1] "2025-10-02 13:53:20 AEST"When date and time data are imported into R they will often default to:
-, : and /candy <- read.csv("../data/candy_production.csv")head(candy,3)
## observation_date IPG3113N## 1 1972-01-01 85.6945## 2 1972-02-01 71.8200## 3 1972-03-01 66.0229class(candy$observation_date)
## [1] "character"candy <- read.csv("../data/candy_production.csv", stringsAsFactors = TRUE)head(candy,3)
## observation_date IPG3113N## 1 1972-01-01 85.6945## 2 1972-02-01 71.8200## 3 1972-03-01 66.0229class(candy$observation_date)
## [1] "factor"library(lubridate)head(lakers[, 1:4], 3)
## date opponent game_type time## 1 20081028 POR home 12:00## 2 20081028 POR home 11:39## 3 20081028 POR home 11:37class(lakers$date)
## [1] "integer"We can use different strategies to convert them to a date format:
as.Date() function under Base R.lubridate package functionsas.Date()candy <- read.csv("../data/candy_production.csv", stringsAsFactors = TRUE)head(candy,3)
## observation_date IPG3113N## 1 1972-01-01 85.6945## 2 1972-02-01 71.8200## 3 1972-03-01 66.0229class(candy$observation_date)
## [1] "factor" candy_date <- as.Date(candy$observation_date) class(candy_date)
## [1] "Date"as.Date()candy <- read.csv("../data/candy_production.csv", stringsAsFactors = FALSE)head(candy,3)
## observation_date IPG3113N## 1 1972-01-01 85.6945## 2 1972-02-01 71.8200## 3 1972-03-01 66.0229class(candy$observation_date)
## [1] "character" candy_date <- as.Date(candy$observation_date) class(candy_date)
## [1] "Date"Note that the default date format is YYYY-MM-DD; therefore, if your string is of different format you must incorporate the format argument.
Have a look at these two examples:
x <- c("08/03/2018", "23/03/2016", "30/01/2018")y <- c("08.03.2018", "23.03.2016", "30.01.2018")
x and DD.MM.YYYY for y; therefore, we need to specify the format argument explicitly. x_date <- as.Date(x, format = "%d/%m/%Y") y_date <- as.Date(y, format = "%d.%m.%Y")
lubridatelubridate package can automatically recognise the common separators used when recording dates (-, /, ., and ). a <- c("26/03/2016", "08/12/2011", "05/01/2013")
a into a date, we can use dmy() (Day, Month, Year) library(lubridate) a_date <- dmy(a) a_date
## [1] "2016-03-26" "2011-12-08" "2013-01-05"lubridate| Function | Order of elements in date-time |
|---|---|
| ymd() | year, month, day |
| ydm() | year, day, month |
| mdy() | month, day, year |
| dmy() | day, month, year |
| hm() | hour, minute |
| hms() | hour, minute, second |
| ymd_hms() | year, month, day, hour, minute, second |
as.Date() and then lubridate functions.task1 <- c("08032018", "29062017", "23032016", "30012018")
task2 <- c("08}03}2018", "29}06}2017", "23}03}2016", "30}01}2018")
task3 <- c("08.03.2018", "29062017", "23/03/2016", "30-01-2018")task1 <- c("08032018", "29062017", "23032016", "30012018")# using as.Datedate_task1 <- as.Date(task1, format = "%d%m%Y")class(date_task1)
## [1] "Date"# using lubridatedate_task1 <- dmy(task1)class(date_task1)
## [1] "Date"task2 <- c("08}03}2018", "29}06}2017", "23}03}2016", "30}01}2018")# using as.Datedate_task2 <- as.Date(task2, format = "%d}%m}%Y")class(date_task2)
## [1] "Date"# using lubridatedate_task2 <- dmy(task2)class(date_task2)
## [1] "Date"task3 <- c("08.03.2018", "29062017", "23/03/2016", "30-01-2018")# using as.Datedate_task3 <- as.Date(task3, format = c("%d.%m.%Y", "%d%m%Y", "%d/%m/%Y", "%d-%m-%Y"))date_task3
## [1] "2018-03-08" "2017-06-29" "2016-03-23" "2018-01-30"class(date_task3)
## [1] "Date"# using lubridatedate_task3 <- dmy(task3)class(date_task3)
## [1] "Date"Sometimes, instead of a single string, we will have date-time spread across multiple columns.
Remember the flights data in the nycflights13 package.
library(nycflights13)head(flights[,c(1:3, 17:18)])
## # A tibble: 6 × 5## year month day hour minute## <int> <int> <int> <dbl> <dbl>## 1 2013 1 1 5 15## 2 2013 1 1 5 29## 3 2013 1 1 5 40## 4 2013 1 1 5 45## 5 2013 1 1 6 0## 6 2013 1 1 5 58make_date() for dates and make_datetime() for date-times.## # A tibble: 6 × 5## year month day hour minute## <int> <int> <int> <dbl> <dbl>## 1 2013 1 1 5 15## 2 2013 1 1 5 29## 3 2013 1 1 5 40## 4 2013 1 1 5 45## 5 2013 1 1 6 0## 6 2013 1 1 5 58flights_new<- flights %>% mutate(departure = make_datetime(year, month, day, hour, minute)) head(flights_new$departure)
## [1] "2013-01-01 05:15:00 UTC" "2013-01-01 05:29:00 UTC"## [3] "2013-01-01 05:40:00 UTC" "2013-01-01 05:45:00 UTC"## [5] "2013-01-01 06:00:00 UTC" "2013-01-01 05:58:00 UTC"lubridate. Here is the list of available functions:| Accessor Function | Extracts |
|---|---|
| year() | year |
| month() | month |
| mday() | day of the month |
| yday() | day of the year |
| wday() | day of the week |
| hour() | hour |
| minute() | minute |
| second() | second |
flights_new$departure column we can use:flights_new$departure %>% year() %>% head()
## [1] 2013 2013 2013 2013 2013 2013# create a datedatetime <- ymd_hms("2016-07-12 12:34:56")# replace the year component with 2025year(datetime) <- 2025# replace the month component with Januarymonth(datetime) <- 05datetime
## [1] "2025-05-12 12:34:56 UTC"We may require to compute a new variable from the date - time information.
To calculate time difference, simply subtract two dates:
my_age <- today() - ymd(19810529)my_age
## Time difference of 16197 daysdifftime() functionmy_age <- difftime(today(), ymd(19810529))my_age
## Time difference of 16197 daysdifftime in R) units argument:my_age_secs<-difftime(today(), ymd(19810529), units = "secs")my_age_secs
## Time difference of 1399420800 secsunits can be "auto", "secs", "mins", "hours", "days", "weeks".
Note that difftime objects do not accept 'years' as a value for 'units', due to "leap years".
To get my age in years:
my_age_days<-difftime(today(), ymd(19810529), units = "days")my_age_years<- as.numeric(my_age_days/365) my_age_years
## [1] 44.37534seq() function with specifying four arguments seq(from, to, by, length.out).# create a sequence of years from 1980 to 2024 by 2even_years <- seq(from = 1980, to=2024, by = 2)even_years
## [1] 1980 1982 1984 1986 1988 1990 1992 1994 1996 1998 2000 2002 2004 2006 2008## [16] 2010 2012 2014 2016 2018 2020 2022 2024# create a sequence of hours from 1/1/2024 9:00 to 1/1/2024 12:00hour_list <- seq(ymd_hm("2024-1-1 9:00"), ymd_hm("2024-1-1 12:00"), by = "hour")hour_list
## [1] "2024-01-01 09:00:00 UTC" "2024-01-01 10:00:00 UTC"## [3] "2024-01-01 11:00:00 UTC" "2024-01-01 12:00:00 UTC"lubridate provides an alternative which always uses seconds: the duration.| Duration Function | Usage |
|---|---|
| as.duration() | assigns duration in seconds |
| is.duration() | check if it is duration |
| dseconds | duration in seconds |
| dminutes | duration in minutes |
| dhours | duration in hours |
| ddays | duration in days |
| dweeks | duration in weeks |
| dyears | duration in years |
# three weeks latertoday() + dweeks(3)
## [1] "2025-10-23"# tomorrowtoday() + ddays(1)
## [1] "2025-10-03"# last yeartoday() - dyears(1)
## [1] "2024-10-01 18:00:00 UTC"# two years, 12 weeks and 15 hours beforetoday() - ( dyears(2) + dweeks(12) + dhours(15) )
## [1] "2023-07-09 21:00:00 UTC"#same as above:today() - ( 2*dyears(1) + dweeks(12) + dhours(15) )
## [1] "2023-07-09 21:00:00 UTC"task1 <- today() + dweeks(0:9)task1
## [1] "2025-10-02" "2025-10-09" "2025-10-16" "2025-10-23" "2025-10-30"## [6] "2025-11-06" "2025-11-13" "2025-11-20" "2025-11-27" "2025-12-04"task2 <- wday(dmy(29051981), label = T)task2
## [1] Fri## Levels: Sun < Mon < Tue < Wed < Thu < Fri < SatDate/time manipulations using BaseR and lubridate
as.Date(), ymd(), ydm(), mdy()...etc.year(), month(), mday(), yday(), wday(), ...etc.duration(), dminutes() , dhours(), ..etcPractice!

Base R has functions to get the current date and time.
The lubridate package offers fast and user-friendly parsing of date-time data.
# get date informationSys.Date()
## [1] "2025-10-02"# get current timeSys.time()
## [1] "2025-10-02 13:53:20 AEST"#install.packages("lubridate")library(lubridate)
# get current time using `lubridate`now()
## [1] "2025-10-02 13:53:20 AEST"Keyboard shortcuts
| ↑, ←, Pg Up, k | Go to previous slide |
| ↓, →, Pg Dn, Space, j | Go to next slide |
| Home | Go to first slide |
| End | Go to last slide |
| Number + Return | Go to specific slide |
| b / m / f | Toggle blackout / mirrored / fullscreen mode |
| c | Clone slideshow |
| p | Toggle presenter mode |
| t | Restart the presentation timer |
| ?, h | Toggle this help |
| Esc | Back to slideshow |