x <- as.Date("1981-02-12")
x
[1] "1981-02-12"
class(x)
[1] "Date"
R includes support for working with date and time data. There are three builtin classes:
Date
: Represents date information (not time)POSIXct
: Represents date & time information as the signed number of seconds since January 1, 1970 (Unix Time a.k.a. POSIX time a.k.a. Epoch time)POSIXlt
: Represents date & time information as a named list (See base::DateTimeClasses
)The data.table package includes the following classes:
IDate
: Similar to the base Date
class, but with a more efficient internal representation.ITime
: Represents time information as the integer number of seconds since midnight.data.table’s fread()
can automatically detect some date and date-time formats and will convert them to IDate
and POSIXct
respectively.
Background info: Portable Operating System Interface (POSIX) is a set of standards for maintaining compatibility among operating systems.
This list serves as a reference for formatting functions later in the chapter.
%Y
: Year with century, (0-9999 accepted) e.g. 2020
%y
: 2-digit year, e.g. 22
%m
: Month, 01-12, e.g. 03
%d
: Day, 01-31, e.g. 04
%H
: Hours, 00-23, e.g. 13
%I
: Hours, 01-12, e.g. 01
%M
: Minutes, 00-59, e.g. 38
%S
: Seconds, 00-61 (sic!) allowing for up to two leap seconds, e.g. 54
There are many more specifications available, see the Details section in the documentation for strptime()
.
Note that some conversions are locale-specific, i.e. will not work the same across systems.
Regarding the ambiguous %y
, the documentation states: “%y
Year without century (00–99). On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 – that is the behaviour specified by the 2018 POSIX standard, but it does also say ‘it is expected that in a future version the default century inferred from a 2-digit year will change’.”
as.Date()
You can create a Date
object from a character:
The tryFormats
argument defines which format(s) are recognized.
The default is tryFormats = c("%Y-%m-%d", "%Y/%m/%d")
, i.e. will recognize dates of the form “2020-11-16” or “2020/11/16”.
Let’s see what happens if a date format is not recognized. Consider the 16th of November, 2021 written as follows:
z <- "11.09.21"
Results in an error if passed to as.Date()
:
zt <- as.Date(z)
Error in charToDate(x): character string is not in a standard unambiguous format
Passing the appropriate format, will allow the date to be read correctly:
zt <- as.Date(z, tryFormats = "%m.%d.%y")
You can convert to data.table’s IDate class using as.IDate()
:
as.IDate()
supports extra arguments just like base as.Date()
. Using the same example as above, we can specify the same tryFormats
argument:
It’s always safest to specify the format of your date string explicitly.
Get current date:
Get current date and time:
Get local timezone:
[1] "America/Los_Angeles"
The reason we use special date and date-time classes is because they allow us to perform mathematical operations on them.
For example, we can subtract date objects to get time intervals:
Note: While you can use the subtraction operator -
, it is advised you use the difftime()
function to perform subtraction on dates instead, because it allows you to specify units.
timepoint1 <- as.Date("2020-01-07")
timepoint2 <- as.Date("2020-02-03")
difftime(timepoint2, timepoint1, units = "weeks")
Time difference of 3.857143 weeks
difftime(timepoint2, timepoint1, units = "days")
Time difference of 27 days
difftime(timepoint2, timepoint1, units = "hours")
Time difference of 648 hours
difftime(timepoint2, timepoint1, units = "mins")
Time difference of 38880 mins
difftime(timepoint2, timepoint1, units = "secs")
Time difference of 2332800 secs
Why is there no option for “months” or “years” in units?
Because, unlike seconds, minutes, hours, days, and weeks, months and years do not have fixed length, i.e. literally a month or a year are not “units” of time.
You can always get a difference in days and divide by 365.2422.
Time difference of 19827 days
difftime()
outputs objects of class difftime
:
class(Age)
[1] "difftime"
If you convert the output of difftime()
using an appropriate mathematical operation, e.g. division, the units will remain, even though they are no longer correct. Use as.numeric()
to convert the difftime object to a regular numeric vector and remove the units which are no longer valid.
Note that the units remain after a mathematical operation:
Age <- Age / 365.2422
Age
Time difference of 54.28453 days
Eliminate them by converting to numeric:
Age <- as.numeric(difftime(Sys.Date(), DOB, units = "days") / 365.2422)
Age
[1] 54.32012
[1] "1985-01-02" "1985-09-02" "1986-05-24" "1989-02-22"
To verify the median, we can do a mathematical operations using multiplication, subtraction, and addition, and the result is still a Date(!):
median_date_too <- x[2] + 0.5 * (x[3] - x[2])
You can create a sequence of dates using seq()
.
If an integer is passed to by
, the unit is assumed to be days:
start_date <- as.Date("2020-09-14")
end_date <- as.Date("2020-12-07")
seq(from = start_date, to = end_date, by = 7)
[1] "2020-09-14" "2020-09-21" "2020-09-28" "2020-10-05" "2020-10-12"
[6] "2020-10-19" "2020-10-26" "2020-11-02" "2020-11-09" "2020-11-16"
[11] "2020-11-23" "2020-11-30" "2020-12-07"
Unlike mathematical operations like difftime()
which require strict units of time, seq()
can work with months and years.
Argument by
can be one of:
“day”, “week”, “month”, “quarter”, “year”.
The above is therefore equivalent to:
seq(from = start_date, to = end_date, by = "week")
[1] "2020-09-14" "2020-09-21" "2020-09-28" "2020-10-05" "2020-10-12"
[6] "2020-10-19" "2020-10-26" "2020-11-02" "2020-11-09" "2020-11-16"
[11] "2020-11-23" "2020-11-30" "2020-12-07"
As with numeric sequences, you can also define the length.out
argument:
[1] "2020-01-20" "2021-01-20" "2022-01-20" "2023-01-20"
An integer can be provided as part of character input to by
:
as.POSIXct()
, as.POSIXlt()
:As always, it can be very informative to look at the source code. Many of these functions call each other internally.
Read strptime()
’s documentation for conversion specifications. These define the order and format of characters to be read as year, month, day, hour, minute, and second information.
For example, the international ISO 8601 standard is defined as:"%Y-%m-%d %H:%M:%S"
Use attributes()
to see the difference between the POSIXct
and POSIXlt
classes:
dt_posixct <- as.POSIXct(dt)
dt_posixct
[1] "2020-03-04 13:38:54 PST"
class(dt_posixct)
[1] "POSIXct" "POSIXt"
str(dt_posixct)
POSIXct[1:1], format: "2020-03-04 13:38:54"
attributes(dt_posixct)
$class
[1] "POSIXct" "POSIXt"
$tzone
[1] ""
dt_posixlt <- as.POSIXlt(dt)
dt_posixlt
[1] "2020-03-04 13:38:54 PST"
class(dt_posixlt)
[1] "POSIXlt" "POSIXt"
str(dt_posixlt)
POSIXlt[1:1], format: "2020-03-04 13:38:54"
dt_posixlt$year
[1] 120
attributes(dt_posixlt)
$names
[1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday"
[9] "isdst" "zone" "gmtoff"
$class
[1] "POSIXlt" "POSIXt"
$tzone
[1] "" "PST" "PDT"
$balanced
[1] TRUE
You can compose a large number of combinations of specification to match your data.
dt2 <- c("03.04.20 01:38.54 pm")
dt2_posix <- as.POSIXct(dt2, format = "%m.%d.%y %I:%M.%S %p")
dt2_posix
[1] "2020-03-04 13:38:54 PST"
format()
Dateformat()
operates on Date and POSIX objects to convert between representations
Define Date in US format:
dt_us <- as.Date("07-04-2020", format = "%m-%d-%Y")
dt_us
[1] "2020-07-04"
Convert to European format:
dt_eu <- format(dt_us, "%d.%m.%y")
dt_eu
[1] "04.07.20"
format()
POSIXctdt <- as.POSIXct("2020-03-04 13:38:54")
dt
[1] "2020-03-04 13:38:54 PST"
format(dt, "%m/%d/%Y @ %H:%M:%S")
[1] "03/04/2020 @ 13:38:54"
To get the relevant R documentation pages, use ?format.Date
and ?format.POSIXct
. To learn more about S3 classes and methods, see Chapter 33.
R includes convenient functions to extract particular seasonal information
[1] "2020-01-05" "2020-02-07" "2020-04-07" "2020-07-22"
R supports timezones. You can see the current timezone using Sys.timezone()
:
[1] "America/Los_Angeles"
Get current date and time:
now <- Sys.time()
now
[1] "2023-11-29 22:44:47 PST"
Get a list of all available timezones:
[1] "Africa/Abidjan" "Africa/Accra"
[3] "Africa/Addis_Ababa" "Africa/Algiers"
[5] "Africa/Asmara" "Africa/Asmera"
[7] "Africa/Bamako" "Africa/Bangui"
[9] "Africa/Banjul" "Africa/Bissau"
[11] "Africa/Blantyre" "Africa/Brazzaville"
[13] "Africa/Bujumbura" "Africa/Cairo"
[15] "Africa/Casablanca" "Africa/Ceuta"
[17] "Africa/Conakry" "Africa/Dakar"
[19] "Africa/Dar_es_Salaam" "Africa/Djibouti"
[21] "Africa/Douala" "Africa/El_Aaiun"
[23] "Africa/Freetown" "Africa/Gaborone"
[25] "Africa/Harare" "Africa/Johannesburg"
[27] "Africa/Juba" "Africa/Kampala"
[29] "Africa/Khartoum" "Africa/Kigali"
[31] "Africa/Kinshasa" "Africa/Lagos"
[33] "Africa/Libreville" "Africa/Lome"
[35] "Africa/Luanda" "Africa/Lubumbashi"
[37] "Africa/Lusaka" "Africa/Malabo"
[39] "Africa/Maputo" "Africa/Maseru"
[41] "Africa/Mbabane" "Africa/Mogadishu"
[43] "Africa/Monrovia" "Africa/Nairobi"
[45] "Africa/Ndjamena" "Africa/Niamey"
[47] "Africa/Nouakchott" "Africa/Ouagadougou"
[49] "Africa/Porto-Novo" "Africa/Sao_Tome"
[51] "Africa/Timbuktu" "Africa/Tripoli"
[53] "Africa/Tunis" "Africa/Windhoek"
[55] "America/Adak" "America/Anchorage"
[57] "America/Anguilla" "America/Antigua"
[59] "America/Araguaina" "America/Argentina/Buenos_Aires"
[61] "America/Argentina/Catamarca" "America/Argentina/ComodRivadavia"
[63] "America/Argentina/Cordoba" "America/Argentina/Jujuy"
[65] "America/Argentina/La_Rioja" "America/Argentina/Mendoza"
[67] "America/Argentina/Rio_Gallegos" "America/Argentina/Salta"
[69] "America/Argentina/San_Juan" "America/Argentina/San_Luis"
[71] "America/Argentina/Tucuman" "America/Argentina/Ushuaia"
[73] "America/Aruba" "America/Asuncion"
[75] "America/Atikokan" "America/Atka"
[77] "America/Bahia" "America/Bahia_Banderas"
[79] "America/Barbados" "America/Belem"
[81] "America/Belize" "America/Blanc-Sablon"
[83] "America/Boa_Vista" "America/Bogota"
[85] "America/Boise" "America/Buenos_Aires"
[87] "America/Cambridge_Bay" "America/Campo_Grande"
[89] "America/Cancun" "America/Caracas"
[91] "America/Catamarca" "America/Cayenne"
[93] "America/Cayman" "America/Chicago"
[95] "America/Chihuahua" "America/Ciudad_Juarez"
[97] "America/Coral_Harbour" "America/Cordoba"
[99] "America/Costa_Rica" "America/Creston"
[101] "America/Cuiaba" "America/Curacao"
[103] "America/Danmarkshavn" "America/Dawson"
[105] "America/Dawson_Creek" "America/Denver"
[107] "America/Detroit" "America/Dominica"
[109] "America/Edmonton" "America/Eirunepe"
[111] "America/El_Salvador" "America/Ensenada"
[113] "America/Fort_Nelson" "America/Fort_Wayne"
[115] "America/Fortaleza" "America/Glace_Bay"
[117] "America/Godthab" "America/Goose_Bay"
[119] "America/Grand_Turk" "America/Grenada"
[121] "America/Guadeloupe" "America/Guatemala"
[123] "America/Guayaquil" "America/Guyana"
[125] "America/Halifax" "America/Havana"
[127] "America/Hermosillo" "America/Indiana/Indianapolis"
[129] "America/Indiana/Knox" "America/Indiana/Marengo"
[131] "America/Indiana/Petersburg" "America/Indiana/Tell_City"
[133] "America/Indiana/Vevay" "America/Indiana/Vincennes"
[135] "America/Indiana/Winamac" "America/Indianapolis"
[137] "America/Inuvik" "America/Iqaluit"
[139] "America/Jamaica" "America/Jujuy"
[141] "America/Juneau" "America/Kentucky/Louisville"
[143] "America/Kentucky/Monticello" "America/Knox_IN"
[145] "America/Kralendijk" "America/La_Paz"
[147] "America/Lima" "America/Los_Angeles"
[149] "America/Louisville" "America/Lower_Princes"
[151] "America/Maceio" "America/Managua"
[153] "America/Manaus" "America/Marigot"
[155] "America/Martinique" "America/Matamoros"
[157] "America/Mazatlan" "America/Mendoza"
[159] "America/Menominee" "America/Merida"
[161] "America/Metlakatla" "America/Mexico_City"
[163] "America/Miquelon" "America/Moncton"
[165] "America/Monterrey" "America/Montevideo"
[167] "America/Montreal" "America/Montserrat"
[169] "America/Nassau" "America/New_York"
[171] "America/Nipigon" "America/Nome"
[173] "America/Noronha" "America/North_Dakota/Beulah"
[175] "America/North_Dakota/Center" "America/North_Dakota/New_Salem"
[177] "America/Nuuk" "America/Ojinaga"
[179] "America/Panama" "America/Pangnirtung"
[181] "America/Paramaribo" "America/Phoenix"
[183] "America/Port_of_Spain" "America/Port-au-Prince"
[185] "America/Porto_Acre" "America/Porto_Velho"
[187] "America/Puerto_Rico" "America/Punta_Arenas"
[189] "America/Rainy_River" "America/Rankin_Inlet"
[191] "America/Recife" "America/Regina"
[193] "America/Resolute" "America/Rio_Branco"
[195] "America/Rosario" "America/Santa_Isabel"
[197] "America/Santarem" "America/Santiago"
[199] "America/Santo_Domingo" "America/Sao_Paulo"
[201] "America/Scoresbysund" "America/Shiprock"
[203] "America/Sitka" "America/St_Barthelemy"
[205] "America/St_Johns" "America/St_Kitts"
[207] "America/St_Lucia" "America/St_Thomas"
[209] "America/St_Vincent" "America/Swift_Current"
[211] "America/Tegucigalpa" "America/Thule"
[213] "America/Thunder_Bay" "America/Tijuana"
[215] "America/Toronto" "America/Tortola"
[217] "America/Vancouver" "America/Virgin"
[219] "America/Whitehorse" "America/Winnipeg"
[221] "America/Yakutat" "America/Yellowknife"
[223] "Antarctica/Casey" "Antarctica/Davis"
[225] "Antarctica/DumontDUrville" "Antarctica/Macquarie"
[227] "Antarctica/Mawson" "Antarctica/McMurdo"
[229] "Antarctica/Palmer" "Antarctica/Rothera"
[231] "Antarctica/South_Pole" "Antarctica/Syowa"
[233] "Antarctica/Troll" "Antarctica/Vostok"
[235] "Arctic/Longyearbyen" "Asia/Aden"
[237] "Asia/Almaty" "Asia/Amman"
[239] "Asia/Anadyr" "Asia/Aqtau"
[241] "Asia/Aqtobe" "Asia/Ashgabat"
[243] "Asia/Ashkhabad" "Asia/Atyrau"
[245] "Asia/Baghdad" "Asia/Bahrain"
[247] "Asia/Baku" "Asia/Bangkok"
[249] "Asia/Barnaul" "Asia/Beirut"
[251] "Asia/Bishkek" "Asia/Brunei"
[253] "Asia/Calcutta" "Asia/Chita"
[255] "Asia/Choibalsan" "Asia/Chongqing"
[257] "Asia/Chungking" "Asia/Colombo"
[259] "Asia/Dacca" "Asia/Damascus"
[261] "Asia/Dhaka" "Asia/Dili"
[263] "Asia/Dubai" "Asia/Dushanbe"
[265] "Asia/Famagusta" "Asia/Gaza"
[267] "Asia/Harbin" "Asia/Hebron"
[269] "Asia/Ho_Chi_Minh" "Asia/Hong_Kong"
[271] "Asia/Hovd" "Asia/Irkutsk"
[273] "Asia/Istanbul" "Asia/Jakarta"
[275] "Asia/Jayapura" "Asia/Jerusalem"
[277] "Asia/Kabul" "Asia/Kamchatka"
[279] "Asia/Karachi" "Asia/Kashgar"
[281] "Asia/Kathmandu" "Asia/Katmandu"
[283] "Asia/Khandyga" "Asia/Kolkata"
[285] "Asia/Krasnoyarsk" "Asia/Kuala_Lumpur"
[287] "Asia/Kuching" "Asia/Kuwait"
[289] "Asia/Macao" "Asia/Macau"
[291] "Asia/Magadan" "Asia/Makassar"
[293] "Asia/Manila" "Asia/Muscat"
[295] "Asia/Nicosia" "Asia/Novokuznetsk"
[297] "Asia/Novosibirsk" "Asia/Omsk"
[299] "Asia/Oral" "Asia/Phnom_Penh"
[301] "Asia/Pontianak" "Asia/Pyongyang"
[303] "Asia/Qatar" "Asia/Qostanay"
[305] "Asia/Qyzylorda" "Asia/Rangoon"
[307] "Asia/Riyadh" "Asia/Saigon"
[309] "Asia/Sakhalin" "Asia/Samarkand"
[311] "Asia/Seoul" "Asia/Shanghai"
[313] "Asia/Singapore" "Asia/Srednekolymsk"
[315] "Asia/Taipei" "Asia/Tashkent"
[317] "Asia/Tbilisi" "Asia/Tehran"
[319] "Asia/Tel_Aviv" "Asia/Thimbu"
[321] "Asia/Thimphu" "Asia/Tokyo"
[323] "Asia/Tomsk" "Asia/Ujung_Pandang"
[325] "Asia/Ulaanbaatar" "Asia/Ulan_Bator"
[327] "Asia/Urumqi" "Asia/Ust-Nera"
[329] "Asia/Vientiane" "Asia/Vladivostok"
[331] "Asia/Yakutsk" "Asia/Yangon"
[333] "Asia/Yekaterinburg" "Asia/Yerevan"
[335] "Atlantic/Azores" "Atlantic/Bermuda"
[337] "Atlantic/Canary" "Atlantic/Cape_Verde"
[339] "Atlantic/Faeroe" "Atlantic/Faroe"
[341] "Atlantic/Jan_Mayen" "Atlantic/Madeira"
[343] "Atlantic/Reykjavik" "Atlantic/South_Georgia"
[345] "Atlantic/St_Helena" "Atlantic/Stanley"
[347] "Australia/ACT" "Australia/Adelaide"
[349] "Australia/Brisbane" "Australia/Broken_Hill"
[351] "Australia/Canberra" "Australia/Currie"
[353] "Australia/Darwin" "Australia/Eucla"
[355] "Australia/Hobart" "Australia/LHI"
[357] "Australia/Lindeman" "Australia/Lord_Howe"
[359] "Australia/Melbourne" "Australia/North"
[361] "Australia/NSW" "Australia/Perth"
[363] "Australia/Queensland" "Australia/South"
[365] "Australia/Sydney" "Australia/Tasmania"
[367] "Australia/Victoria" "Australia/West"
[369] "Australia/Yancowinna" "Brazil/Acre"
[371] "Brazil/DeNoronha" "Brazil/East"
[373] "Brazil/West" "Canada/Atlantic"
[375] "Canada/Central" "Canada/Eastern"
[377] "Canada/Mountain" "Canada/Newfoundland"
[379] "Canada/Pacific" "Canada/Saskatchewan"
[381] "Canada/Yukon" "CET"
[383] "Chile/Continental" "Chile/EasterIsland"
[385] "CST6CDT" "Cuba"
[387] "EET" "Egypt"
[389] "Eire" "EST"
[391] "EST5EDT" "Etc/GMT"
[393] "Etc/GMT-0" "Etc/GMT-1"
[395] "Etc/GMT-10" "Etc/GMT-11"
[397] "Etc/GMT-12" "Etc/GMT-13"
[399] "Etc/GMT-14" "Etc/GMT-2"
[401] "Etc/GMT-3" "Etc/GMT-4"
[403] "Etc/GMT-5" "Etc/GMT-6"
[405] "Etc/GMT-7" "Etc/GMT-8"
[407] "Etc/GMT-9" "Etc/GMT+0"
[409] "Etc/GMT+1" "Etc/GMT+10"
[411] "Etc/GMT+11" "Etc/GMT+12"
[413] "Etc/GMT+2" "Etc/GMT+3"
[415] "Etc/GMT+4" "Etc/GMT+5"
[417] "Etc/GMT+6" "Etc/GMT+7"
[419] "Etc/GMT+8" "Etc/GMT+9"
[421] "Etc/GMT0" "Etc/Greenwich"
[423] "Etc/UCT" "Etc/Universal"
[425] "Etc/UTC" "Etc/Zulu"
[427] "Europe/Amsterdam" "Europe/Andorra"
[429] "Europe/Astrakhan" "Europe/Athens"
[431] "Europe/Belfast" "Europe/Belgrade"
[433] "Europe/Berlin" "Europe/Bratislava"
[435] "Europe/Brussels" "Europe/Bucharest"
[437] "Europe/Budapest" "Europe/Busingen"
[439] "Europe/Chisinau" "Europe/Copenhagen"
[441] "Europe/Dublin" "Europe/Gibraltar"
[443] "Europe/Guernsey" "Europe/Helsinki"
[445] "Europe/Isle_of_Man" "Europe/Istanbul"
[447] "Europe/Jersey" "Europe/Kaliningrad"
[449] "Europe/Kiev" "Europe/Kirov"
[451] "Europe/Kyiv" "Europe/Lisbon"
[453] "Europe/Ljubljana" "Europe/London"
[455] "Europe/Luxembourg" "Europe/Madrid"
[457] "Europe/Malta" "Europe/Mariehamn"
[459] "Europe/Minsk" "Europe/Monaco"
[461] "Europe/Moscow" "Europe/Nicosia"
[463] "Europe/Oslo" "Europe/Paris"
[465] "Europe/Podgorica" "Europe/Prague"
[467] "Europe/Riga" "Europe/Rome"
[469] "Europe/Samara" "Europe/San_Marino"
[471] "Europe/Sarajevo" "Europe/Saratov"
[473] "Europe/Simferopol" "Europe/Skopje"
[475] "Europe/Sofia" "Europe/Stockholm"
[477] "Europe/Tallinn" "Europe/Tirane"
[479] "Europe/Tiraspol" "Europe/Ulyanovsk"
[481] "Europe/Uzhgorod" "Europe/Vaduz"
[483] "Europe/Vatican" "Europe/Vienna"
[485] "Europe/Vilnius" "Europe/Volgograd"
[487] "Europe/Warsaw" "Europe/Zagreb"
[489] "Europe/Zaporozhye" "Europe/Zurich"
[491] "GB" "GB-Eire"
[493] "GMT" "GMT-0"
[495] "GMT+0" "GMT0"
[497] "Greenwich" "Hongkong"
[499] "HST" "Iceland"
[501] "Indian/Antananarivo" "Indian/Chagos"
[503] "Indian/Christmas" "Indian/Cocos"
[505] "Indian/Comoro" "Indian/Kerguelen"
[507] "Indian/Mahe" "Indian/Maldives"
[509] "Indian/Mauritius" "Indian/Mayotte"
[511] "Indian/Reunion" "Iran"
[513] "Israel" "Jamaica"
[515] "Japan" "Kwajalein"
[517] "Libya" "MET"
[519] "Mexico/BajaNorte" "Mexico/BajaSur"
[521] "Mexico/General" "MST"
[523] "MST7MDT" "Navajo"
[525] "NZ" "NZ-CHAT"
[527] "Pacific/Apia" "Pacific/Auckland"
[529] "Pacific/Bougainville" "Pacific/Chatham"
[531] "Pacific/Chuuk" "Pacific/Easter"
[533] "Pacific/Efate" "Pacific/Enderbury"
[535] "Pacific/Fakaofo" "Pacific/Fiji"
[537] "Pacific/Funafuti" "Pacific/Galapagos"
[539] "Pacific/Gambier" "Pacific/Guadalcanal"
[541] "Pacific/Guam" "Pacific/Honolulu"
[543] "Pacific/Johnston" "Pacific/Kanton"
[545] "Pacific/Kiritimati" "Pacific/Kosrae"
[547] "Pacific/Kwajalein" "Pacific/Majuro"
[549] "Pacific/Marquesas" "Pacific/Midway"
[551] "Pacific/Nauru" "Pacific/Niue"
[553] "Pacific/Norfolk" "Pacific/Noumea"
[555] "Pacific/Pago_Pago" "Pacific/Palau"
[557] "Pacific/Pitcairn" "Pacific/Pohnpei"
[559] "Pacific/Ponape" "Pacific/Port_Moresby"
[561] "Pacific/Rarotonga" "Pacific/Saipan"
[563] "Pacific/Samoa" "Pacific/Tahiti"
[565] "Pacific/Tarawa" "Pacific/Tongatapu"
[567] "Pacific/Truk" "Pacific/Wake"
[569] "Pacific/Wallis" "Pacific/Yap"
[571] "Poland" "Portugal"
[573] "PRC" "PST8PDT"
[575] "ROC" "ROK"
[577] "Singapore" "Turkey"
[579] "UCT" "Universal"
[581] "US/Alaska" "US/Aleutian"
[583] "US/Arizona" "US/Central"
[585] "US/East-Indiana" "US/Eastern"
[587] "US/Hawaii" "US/Indiana-Starke"
[589] "US/Michigan" "US/Mountain"
[591] "US/Pacific" "US/Samoa"
[593] "UTC" "W-SU"
[595] "WET" "Zulu"
attr(,"Version")
[1] "2023c"
Set to a different timezone:
Sys.setenv(TZ = "America/New_York")
Note how setting the timezone changes how the date and time are printed:
now
[1] "2023-11-30 01:44:47 EST"
Use format()
’s tz
argument to convert date & time to different timezones.
format(now, tz = "Europe/London")
[1] "2023-11-30 06:44:47"
format(now, tz = "Africa/Nairobi")
[1] "2023-11-30 09:44:47"
format(now, tz = "Asia/Tokyo")
[1] "2023-11-30 15:44:47"
format(now, tz = "Australia/Sydney")
[1] "2023-11-30 17:44:47"
Note that the output of format()
is not POSIXct
or POSIXlt
, but a character:
The timezone is not part of the POSIXct
object itself:
attributes(now)
$class
[1] "POSIXct" "POSIXt"
The print()
method for datetime objects calls format()
internally.