3  Dates and Times

Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'tibble' was built under R version 4.5.2
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'readr' was built under R version 4.5.2
Warning: package 'purrr' was built under R version 4.5.2
Warning: package 'stringr' was built under R version 4.5.2
Warning: package 'forcats' was built under R version 4.5.2
Warning: package 'knitr' was built under R version 4.5.2

3.1 Date Formats

String Meaning Code Output
<span style=" font-family: monospace; " >%a</span> Day of the week, abbreviated (Mon-Sun) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%a&quot;)</span> Thu
<span style=" font-family: monospace; " >%A</span> Day of the week, full (Monday-Sunday <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%A&quot;)</span> Thursday
<span style=" font-family: monospace; " >%w</span> Day of the week, numeric, 0 = Sunday (0-6) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%w&quot;)</span> 4
<span style=" font-family: monospace; " >%e</span> Day of month (1-31) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%e&quot;)</span> 10
<span style=" font-family: monospace; " >%d</span> Day of month (01-31) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%d&quot;)</span> 10
<span style=" font-family: monospace; " >%m</span> Month, numeric (01-12) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%m&quot;)</span> 12
<span style=" font-family: monospace; " >%b</span> Month, abbreviated (Jan-Dec) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%b&quot;)</span> Dec
<span style=" font-family: monospace; " >%B</span> Month, full (January-December) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%B&quot;)</span> December
<span style=" font-family: monospace; " >%y</span> Year, without century (00-99) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%y&quot;)</span> 20
<span style=" font-family: monospace; " >%Y</span> Year, with century (0000-9999) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%Y&quot;)</span> 2020
<span style=" font-family: monospace; " >%j</span> Day of the Year (001-366) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%j&quot;)</span> 345
<span style=" font-family: monospace; " >%U</span> Week of year, numeric, starting on Sunday (00-52) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%U&quot;)</span> 49
<span style=" font-family: monospace; " >%W</span> Week of year, numeric, starting on Monday (00-52) <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%W&quot;)</span> 49
<span style=" font-family: monospace; " >%x</span> Locale-specific date <span style=" font-family: monospace; " >format.Date(&quot;2020-12-10&quot;, &quot;%x&quot;)</span> 2020-12-10

3.2 Time Formats

String Meaning Code Output
<span style=" font-family: monospace; " >%S</span> Second (00-59) <span style=" font-family: monospace; " >format.Date(as.POSIXct(&quot;2020-12-10 15:30:10&quot;, tz = &quot;America/Chicago&quot;), &quot;%S&quot;)</span> 10
<span style=" font-family: monospace; " >%M</span> Minute (00-59) <span style=" font-family: monospace; " >format.Date(as.POSIXct(&quot;2020-12-10 15:30:10&quot;, tz = &quot;America/Chicago&quot;), &quot;%M&quot;)</span> 30
<span style=" font-family: monospace; " >%l</span> Hour, in 12-hour clock (1-12) <span style=" font-family: monospace; " >format.Date(as.POSIXct(&quot;2020-12-10 15:30:10&quot;, tz = &quot;America/Chicago&quot;), &quot;%l&quot;)</span> 3
<span style=" font-family: monospace; " >%I</span> Hour, in 12-hour clock (01-12) <span style=" font-family: monospace; " >format.Date(as.POSIXct(&quot;2020-12-10 15:30:10&quot;, tz = &quot;America/Chicago&quot;), &quot;%I&quot;)</span> 03
<span style=" font-family: monospace; " >%p</span> am/pm <span style=" font-family: monospace; " >format.Date(as.POSIXct(&quot;2020-12-10 15:30:10&quot;, tz = &quot;America/Chicago&quot;), &quot;%p&quot;)</span> PM
<span style=" font-family: monospace; " >%H</span> Hour, in 24-hour clock (00-23) <span style=" font-family: monospace; " >format.Date(as.POSIXct(&quot;2020-12-10 15:30:10&quot;, tz = &quot;America/Chicago&quot;), &quot;%H&quot;)</span> 15
<span style=" font-family: monospace; " >%X</span> Locale-specific time <span style=" font-family: monospace; " >format.Date(as.POSIXct(&quot;2020-12-10 15:30:10&quot;, tz = &quot;America/Chicago&quot;), &quot;%X&quot;)</span> 15:30:10
<span style=" font-family: monospace; " >%c</span> Locale-specific date and time <span style=" font-family: monospace; " >format.Date(as.POSIXct(&quot;2020-12-10 15:30:10&quot;, tz = &quot;America/Chicago&quot;), &quot;%c&quot;)</span> Thu Dec 10 15:30:10 2020
<span style=" font-family: monospace; " >%z</span> Offset from GMT <span style=" font-family: monospace; " >format.Date(as.POSIXct(&quot;2020-12-10 15:30:10&quot;, tz = &quot;America/Chicago&quot;), &quot;%z&quot;)</span> -0600
<span style=" font-family: monospace; " >%Z</span> Time zone (character) <span style=" font-family: monospace; " >format.Date(as.POSIXct(&quot;2020-12-10 15:30:10&quot;, tz = &quot;America/Chicago&quot;), &quot;%Z&quot;)</span> CST

3.3 Time Zone options in R

The above example uses Central time and so I can use tz = America/Chicago; other timezone options can be found using the code below:

#check system time zone
Sys.timezone(location = TRUE)
[1] "America/Chicago"
#other time zone options (only show first 20)
OlsonNames()[1:20]
 [1] "Africa/Abidjan"       "Africa/Accra"         "Africa/Addis_Ababa"  
 [4] "Africa/Algiers"       "Africa/Asmara"        "Africa/Asmera"       
 [7] "Africa/Bamako"        "Africa/Bangui"        "Africa/Banjul"       
[10] "Africa/Bissau"        "Africa/Blantyre"      "Africa/Brazzaville"  
[13] "Africa/Bujumbura"     "Africa/Cairo"         "Africa/Casablanca"   
[16] "Africa/Ceuta"         "Africa/Conakry"       "Africa/Dakar"        
[19] "Africa/Dar_es_Salaam" "Africa/Djibouti"