2.8 日期
注意观察时间转化
Sys.Date()## [1] "2023-05-25"
Sys.time()## [1] "2023-05-25 04:51:07 UTC"
c(Sys.time(), Sys.Date())## [1] "2023-05-25 04:51:07 UTC" "2023-05-25 00:00:00 UTC"
data.table::year(Sys.Date())## [1] 2023
data.table::year(Sys.time())## [1] 2023
data.table::year(c(Sys.time(), Sys.Date()))## [1] 2023 2023
x <- Sys.time()
class(x)## [1] "POSIXct" "POSIXt"
format(x, format = "%Y-%m-%d")## [1] "2023-05-25"
x <- c("2019-12-21", "2019/12/21")
data.table::year("2019-12-21")## [1] 2019
data.table::year("2019/12/21")## [1] 2019
但是,下面这样会报错
data.table::year(x)## Error in as.POSIXlt.character(x): character string is not in a standard unambiguous format
正确的姿势是首先将表示日期的字符串格式统一
gsub(pattern = "/", replacement = "-", x) |>
data.table::year()## [1] 2019 2019
date-times 表示 POSIXct 和 POSIXlt 类型的日期对象
(x <- Sys.time())## [1] "2023-05-25 04:51:07 UTC"
class(x)## [1] "POSIXct" "POSIXt"
data.table::second(x) # 取秒## [1] 7
format(x, format = "%S")## [1] "07"
data.table::minute(x) # 取分## [1] 51
format(x, format = "%M")## [1] "51"
data.table::hour(x) # 取时## [1] 4
format(x, format = "%H")## [1] "04"
data.table::yday(x) # 此刻在一年的第几天## [1] 145
data.table::wday(x) # 此刻在一周的第几天,星期日是第1天,星期六是第7天## [1] 5
data.table::mday(x) # 此刻在当月第几天## [1] 25
format(x, format = "%d")## [1] "25"
weekdays(x)## [1] "Thursday"
weekdays(x, abbreviate = T)## [1] "Thu"
data.table::week(x) # 此刻在第几周## [1] 21
data.table::isoweek(x)## [1] 21
data.table::month(x) # 此刻在第几月## [1] 5
format(x, format = "%m")## [1] "05"
months(x)## [1] "May"
months(x, abbreviate = T)## [1] "May"
data.table::quarter(x) # 此刻在第几季度## [1] 2
quarters(x)## [1] "Q2"
data.table::year(x) # 取年## [1] 2023
format(x, format = "%Y")## [1] "2023"
format() 是一个泛型函数,此刻命名空间有 101 方法。 format.Date(), format.difftime(), format.POSIXct() 和 format.POSIXlt() 四个函数通过格式化不同类型的日期数据对象抽取指定部分。
format(difftime(Sys.time(), x, units = "secs"))## [1] "0.08600068 secs"
上个季度最后一天
# https://d.cosx.org/d/421162/16
as.Date(cut(as.Date(c("2020-02-01", "2020-05-02")), "quarter")) - 1## [1] "2019-12-31" "2020-03-31"
本季度第一天
as.Date(cut(as.Date(c("2020-02-01", "2020-05-02")), "quarter"))## [1] "2020-01-01" "2020-04-01"
类似地,本月第一天和上月最后一天
# 本月第一天
as.Date(cut(as.Date(c("2020-02-01", "2020-05-02")), "month"))## [1] "2020-02-01" "2020-05-01"
# 上月最后一天
as.Date(cut(as.Date(c("2020-02-01", "2020-05-02")), "month")) - 1## [1] "2020-01-31" "2020-04-30"
timeDate 提供了很多日期计算函数,比如季初、季末、月初、月末等
library(timeDate)
# 季初
as.Date(format(timeFirstDayInQuarter(charvec = c("2020-02-01", "2020-05-02")), format = "%Y-%m-%d"))
# 季末
as.Date(format(timeLastDayInQuarter(charvec = c("2020-02-01", "2020-05-02")), format = "%Y-%m-%d"))
# 月初
as.Date(format(timeFirstDayInMonth(charvec = c("2020-02-01", "2020-05-02")), format = "%Y-%m-%d"))
# 月末
as.Date(format(timeLastDayInMonth(charvec = c("2020-02-01", "2020-05-02")), format = "%Y-%m-%d")) cut.Date() 是一个泛型函数,查看它的所有 S3 方法
methods(cut)## [1] cut.Date cut.default cut.dendrogram* cut.IDate*
## [5] cut.POSIXt
## see '?methods' for accessing help and source code
格式化输出日期类型数据
formatC(round(runif(1, 1e8, 1e9)), digits = 10, big.mark = ",")## [1] "586,715,784"
# Sys.setlocale(locale = "C") # 如果是 Windows 系统,必须先设置,否则转化结果是 NA
as.Date(paste("1990-January", 1, sep = "-"), format = "%Y-%B-%d")## [1] "1990-01-01"
获取当日零点
format(as.POSIXlt(Sys.Date()), "%Y-%m-%d %H:%M:%S")## [1] "2023-05-25 00:00:00"
从 POSIXt 数据对象中,抽取小时和分钟部分,返回字符串
strftime(x = Sys.time(), format = "%H:%M")## [1] "04:51"
| 代码 | 含义 | 代码 | 含义 |
|---|---|---|---|
%a |
Abbreviated weekday | %A |
Full weekday |
%b |
Abbreviated month | %B |
Full month |
%c |
Locale-specific date and time | %d |
Decimal date |
%H |
Decimal hours (24 hour) | %I |
Decimal hours (12 hour) |
%j |
Decimal day of the year | %m |
Decimal month |
%M |
Decimal minute | %p |
Locale-specific AM/PM |
%S |
Decimal second | %U |
Decimal week of the year (starting on Sunday) |
%w |
Decimal Weekday (0=Sunday) | %W |
Decimal week of the year (starting on Monday) |
%x |
Locale-specific Date | %X |
Locale-specific Time |
%y |
2-digit year | %Y |
4-digit year |
%z |
Offset from GMT | %Z |
Time zone (character) |
本节介绍了 R 本身提供的基础日期操作,第??章着重介绍一般的时间序列类型的数据对象及其操作。
参考文献
[9]
B. D. Ripley and K. Hornik, “Date-time classes,” R News, vol. 1, no. 2, pp. 8–11, 2001,Available: https://cran.r-project.org/doc/Rnews/Rnews_2001-2.pdf
[10]
G. Grothendieck and T. Petzoldt, “R Help Desk: Date and time classes in R,” R News, vol. 4, no. 1, pp. 29–32, 2004,Available: https://www.r-project.org/doc/Rnews/Rnews_2004-1.pdf