lubridate is the package that handles dates and times, but is now part of the tidyverse, so no need to load separately.
[1] "2026-06-15"
[1] "2026-08-04"
read_csv and the others can guess that you have dates, if you format them as year-month-day, like column 1 of this .csv:date,status,dunno
2011-08-03,hello,August 3 2011
2011-11-15,still here,November 15 2011
2012-02-01,goodbye,February 1 2012
Rows: 3 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): status, dunno
date (1): date
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
dates1 shows that dates are best stored as text in format yyyy-mm-dd (ISO 8601 standard).lubridate functions to convert. For example, dates in US format with month first:uk, except for the last one, which makes no sense as a date written this way.# A tibble: 3 × 3
date status dunno
<date> <chr> <chr>
1 2011-08-03 hello August 3 2011
2 2011-11-15 still here November 15 2011
3 2012-02-01 goodbye February 1 2012
dunno has month (name), day, year in that order.date) with those converted from dunno:# A tibble: 3 × 5
date status dunno date_from_text equal
<date> <chr> <chr> <date> <lgl>
1 2011-08-03 hello August 3 2011 2011-08-03 TRUE
2 2011-11-15 still here November 15 2011 2011-11-15 TRUE
3 2012-02-01 goodbye February 1 2012 2012-02-01 TRUE
We know how to convert text to dates, but what if we want to convert dates to text, for example to display them in a certain format?
Can use strftime (challenging) or stamp (easier).
stampstamp:
stamp…you get a message:
Multiple formats matched: "%d %Om, year %Y"(1), "%d %b, year %Y"(1)
Using: "%d %b, year %Y"
These codes are what strftime uses:
%d is the day of the month (with leading zero)%b is the abbreviated month name%Y is the four-digit yearSee here for all the codes.
Starting from this file:
make-ing a dateymd_hms:tz= and the name of a timezone:OlsonNames(). Some of them: [1] "America/Edmonton" "America/Grenada" "America/Coyhaique"
[4] "Europe/Gibraltar" "America/Rankin_Inlet" "Australia/Sydney"
[7] "Europe/Belgrade" "Pacific/Midway" "Etc/GMT-3"
[10] "Atlantic/Reykjavik"
# A tibble: 4 × 5
dt h sec min zone
<dttm> <int> <dbl> <int> <chr>
1 1970-01-01 07:50:01 7 1 50 America/Toronto
2 2007-09-04 15:30:00 15 0 30 America/Toronto
3 1940-04-15 06:45:10 6 10 45 America/Toronto
4 2016-02-10 12:26:40 12 40 26 America/Toronto
dt, the time it will be for them is as shown in oz.[1] "1970-01-01 22:50:01 AEST" "2007-09-05 05:30:00 AEST"
[3] "1940-04-15 21:45:10 AEST" "2016-02-11 04:26:40 AEDT"
“Australian Eastern Time”, Standard or Daylight. Note when the Australian summer is.
admit,discharge
1981-12-10 22:00:00,1982-01-03 14:00:00
2014-03-07 14:00:00,2014-03-08 09:30:00
2016-08-31 21:00:00,2016-09-02 17:00:00
drtn at the top of the column stands for “duration”.day() etc, as for a date-time:
Comments
days(1),months(1)etc.