Date and time in R

Jyoti Kumar
3 min readApr 3, 2022

In this blog, I will discuss one of the most common challenges faced while analyzing data from a file. Most of the times the dates in the files are not in the standard format like yyyy-mm-dd. So the first step after loading data is to convert the date in the standard format.

Let’s discuss how we can convert dates in different format to standard format using base r functions like as.Date, strptime and as.POSIXlt

  1. Conversion of dates to standard format using as.Date function
# Using as.Datesample_dates = c(‘03/11/2021’, ‘03-Nov-2021’, ‘Nov 03 21’, ‘11/03/2021’)date1 = as.Date(sample_dates[1], format = ‘%d/%m/%Y’)date2 = as.Date(sample_dates[2], format = ‘%d-%B-%Y’)date3 = as.Date(sample_dates[3], format = ‘%B %d %y’)date4 = as.Date(sample_dates[4], format = ‘%m/%d/%Y’)print(paste(‘date1:’, date1, ‘,’, ‘date2:’,date2, ‘,’, ‘date3:’,date3, ‘,’, ‘date4:’,date4))

The result is as under:

date1: 2021–11–03 , date2: 2021–11–03 , date3: 2021–11–03 , date4: 2021–11–03

2. Conversion of dates to standard format using strptime function

# Using strptimedate11 = strptime(sample_dates[1], format = ‘%d/%m/%Y’)date21 = strptime(sample_dates[2], format = ‘%d-%B-%Y’)date31 = strptime(sample_dates[3], format = ‘%B %d %y’)date41 = strptime(sample_dates[4], format = ‘%m/%d/%Y’)print(paste(‘date11:’, date11, ‘,’, ‘date21:’,date21, ‘,’, ‘date31:’,date31, ‘,’, ‘date41:’,date41))

The result is as under:

date11: 2021–11–03 , date21: 2021–11–03 , date31: 2021–11–03 , date41: 2021–11–03

3. Conversion of date from one format to another using format function

# Format date from one format to other e.g. ‘03/11/2021’ to “November-03–2021”# Convert date to standard format first# Change format using format functiondate_std = as.Date(‘03/11/2021’, format = ‘%d/%m/%Y’)(date12 = format(date_std, ‘%B-%d-%Y’))

The result is as under:

November-03–2021

4. Calculating date difference between two days using difftime

# Date Differencestd_date = as.Date(c('2022-01-26', '2021-08-15'))# Getting the date difference in days# units can be any of the following - "auto", "secs", "mins", "hours", "days", "weeks"(diff_days = difftime(std_date[1], std_date[2], units = "days"))

The result is as under:

Time difference of 164 days

5. Conversion of date time to standard format using strptime

The time is in 12 hrs format and AM/PM is used. Hence in the format argument I - for hours, M - for minutes and p - for AM/PM is used.

# Date time using strptimestart_dt = ‘2/11/2022 4:00 AM’end_dt = ‘2/11/2022 4:30 PM’(start_dtm = strptime(start_dt, format = ‘%m/%d/%Y %I:%M %p’))(end_dtm = strptime(end_dt, format = ‘%m/%d/%Y %I:%M %p’))

The result is as under:

[1] “2022–02–11 04:00:00 UTC”[1] “2022–02–11 16:30:00 UTC”

The time is in 24 hrs format and AM/PM is not used. Hence in the format argument H— for hours and M — for minutes is used.

# 24 hrs date conversion using strptimestart_dt1 = ‘2/11/2022 4:00’end_dt1 = ‘2/11/2022 16:30’(start_dtm1 = strptime(start_dt1, format = ‘%m/%d/%Y %H:%M’))(end_dtm1 = strptime(end_dt1, format = ‘%m/%d/%Y %H:%M’))

The result is as under:

[1] “2022–02–11 04:00:00 UTC”[1] “2022–02–11 16:30:00 UTC”

6. Conversion of date time to standard format using as.POSIXlt

start_dt1 = ‘2/11/2022 4:00’end_dt1 = ‘2/11/2022 16:30’(start_dtm2 = as.POSIXlt(start_dt1, format = ‘%m/%d/%Y %H:%M’))(end_dtm2 = as.POSIXlt(end_dt1, format = ‘%m/%d/%Y %H:%M’))

The result is as under:

[1] “2022–02–11 04:00:00 UTC”[1] “2022–02–11 16:30:00 UTC”

7. Calculating time difference

# Difference between date timeend_dtm2 — start_dtm2

The result is as under:

Time difference of 12.5 hours

8. Getting year, month and day from a date using format function

date_t = as.Date(‘11/03/2021’, format = ‘%d/%m/%Y’)year = format(date_t, ‘%Y’)month = format(date_t, ‘%B’)day = format(date_t, ‘%d’)paste(‘For date ‘, date_t, ‘: Year — ‘, year, ‘: Month — ‘, month, ‘: Day — ‘, day)

The result is as under:

For date 2021–03–11 : Year — 2021 : Month — Mar : Day — 11

The format of month and year can be changed by changing the argument in the format function.

I have published the code at Date time in R blog (Static report) (jetbrains.com)

The lubridate package from tidyverse is a great package to convert strings to date and do calculations related to date and time.

References:

  1. Dates and Times in R (berkeley.edu)
  2. help(“as.Date”)
  3. help(“strftime”)

--

--

Jyoti Kumar

I have experience in Predictive Modelling and Dashboards. I have rich working experience on various tools and software like Python, R, Tableau, Power BI and SQL