创建
使用日期时间时,1 表示 1 秒,因此 binwidth 为 86400 表示一天
对于日期,1 表示 1 天
Show the code
#字符串捕获
ymd("2017-01-31")
#> [1] "2017-01-31"
mdy("January 31st, 2017")
#> [1] "2017-01-31"
dmy("31-Jan-2017")
#> [1] "2017-01-31"
ymd_hms("2017-01-31 20:11:59")
#> [1] "2017-01-31 20:11:59 UTC"
mdy_hm("01/31/2017 08:01")
#> [1] "2017-01-31 08:01:00 UTC"
Show the code
# 导入文件
csv <- "\ndate,datetime\n2022-01-02,2022-01-02 05:12\n"
read_csv(csv)
2022-01-02 |
2022-01-02 05:12:00 |
Show the code
(csv <- "
date
01/02/15
")
#> [1] "\n date\n 01/02/15\n"
read_csv(csv, col_types = cols(date = col_date("%m/%d/%y")))
Show the code
read_csv(csv, col_types = cols(date = col_date("%y/%m/%d")))
Show the code
#类型转换
as_datetime(60 * 60 * 10)
#> [1] "1970-01-01 10:00:00 UTC"
as_date(365 * 10 + 2)
#> [1] "1980-01-01"
today()
#> [1] "2024-11-19"
as_datetime(today())
#> [1] "2024-11-19 UTC"
now()
#> [1] "2024-11-19 23:36:23 CST"
as_date(now())
#> [1] "2024-11-19"
datetime组件
Show the code
# 获取日期时间组件
dttm <- ymd_hms("2026-07-08 12:34:56")
year(dttm)
#> [1] 2026
month(dttm,label = TRUE)
#> [1] 7月
#> 12 Levels: 1月 < 2月 < 3月 < 4月 < 5月 < 6月 < 7月 < 8月 < 9月 < ... < 12月
mday(dttm)
#> [1] 8
yday(dttm) #一年中的第几天
#> [1] 189
wday(dttm, label = TRUE, abbr = FALSE) #星期几
#> [1] 星期三
#> Levels: 星期日 < 星期一 < 星期二 < 星期三 < 星期四 < 星期五 < 星期六
hour(dttm)
#> [1] 12
minute(dttm)
#> [1] 34
second(dttm)
#> [1] 56
# 修改组件
year(dttm) <- 2030
month(dttm) <- 01
hour(dttm) <- hour(dttm) + 1
dttm
#> [1] "2030-01-08 13:34:56 UTC"
update(dttm, year = 2030, month = 2, mday = 2, hour = 2)
#> [1] "2030-02-02 02:34:56 UTC"
update(ymd("2023-02-01"), mday = 30)
#> [1] "2023-03-02"
update(ymd("2023-02-01"), hour = 400)
#> [1] "2023-02-17 16:00:00 UTC"
Rounding
Show the code
#floor_date()round_date()ceiling_date()
duration
Show the code
# 时间跨度Time spans
# 持续时间 duration 始终以秒为单位记录时间跨度
h_age <- today() - ymd("2000-10-18")
as.duration(h_age)
#> [1] "760147200s (~24.09 years)"
dseconds(15)
#> [1] "15s"
dminutes(10)
#> [1] "600s (~10 minutes)"
dhours(c(12, 24))
#> [1] "43200s (~12 hours)" "86400s (~1 days)"
ddays(0:5)
#> [1] "0s" "86400s (~1 days)" "172800s (~2 days)"
#> [4] "259200s (~3 days)" "345600s (~4 days)" "432000s (~5 days)"
dweeks(3)
#> [1] "1814400s (~3 weeks)"
dyears(1)/ddays(1) #365.25 天
#> [1] 365.25
Periods
Show the code
# Periods “人类”时间(如天和月)
years(1) #365.25 天
#> [1] "1y 0m 0d 0H 0M 0S"
years(1) / days(1)
#> [1] 365.25
hours(c(12, 24))
#> [1] "12H 0M 0S" "24H 0M 0S"
days(7)
#> [1] "7d 0H 0M 0S"
months(1:6)
#> [1] "1m 0d 0H 0M 0S" "2m 0d 0H 0M 0S" "3m 0d 0H 0M 0S" "4m 0d 0H 0M 0S"
#> [5] "5m 0d 0H 0M 0S" "6m 0d 0H 0M 0S"
ymd("2024-01-01") + dyears(1) #闰年
#> [1] "2024-12-31 06:00:00 UTC"
ymd("2024-01-01") + years(1)
#> [1] "2025-01-01"
ymd_hms("2026-03-08 01:00:00", tz = "America/New_York")+ddays(1)
#> [1] "2026-03-09 02:00:00 EDT"
ymd_hms("2026-03-08 01:00:00", tz = "America/New_York")+days(1)
#> [1] "2026-03-09 01:00:00 EDT"
Intervals
Show the code
# start %--% end
y2023 <- ymd("2023-01-01") %--% ymd("2024-01-01")
y2024 <- ymd("2024-01-01") %--% ymd("2025-01-01")
y2023 / days(1)
#> [1] 365
y2024 / days(1)
#> [1] 366
时区
Show the code
Sys.timezone()
#> [1] "Asia/Shanghai"
OlsonNames()
#> [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-au-Prince" "America/Port_of_Spain"
#> [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] "2024a"