3  Dates and Times

3.1 Date Formats

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

3.2 Time Formats

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