R语言日期时间 #
一、日期时间概述 #
R语言提供了多种日期时间类型:
| 类型 | 说明 | 示例 |
|---|---|---|
| Date | 日期 | “2024-01-15” |
| POSIXct | 日期时间(秒数存储) | “2024-01-15 10:30:00” |
| POSIXlt | 日期时间(列表存储) | 包含年月日时分秒的列表 |
二、Date类型 #
2.1 创建日期 #
r
today <- Sys.Date()
print(today)
class(today)
date1 <- as.Date("2024-01-15")
print(date1)
date2 <- as.Date("2024/01/15", format = "%Y/%m/%d")
print(date2)
2.2 日期格式符号 #
| 符号 | 说明 | 示例 |
|---|---|---|
| %Y | 四位年份 | 2024 |
| %y | 两位年份 | 24 |
| %m | 两位月份 | 01-12 |
| %d | 两位日期 | 01-31 |
| %B | 月份名称 | January |
| %b | 月份缩写 | Jan |
| %A | 星期名称 | Monday |
| %a | 星期缩写 | Mon |
2.3 格式化日期 #
r
date <- as.Date("2024-01-15")
format(date, "%Y年%m月%d日")
format(date, "%B %d, %Y")
format(date, "%A")
format(date, "%Y-%m-%d")
2.4 日期计算 #
r
date1 <- as.Date("2024-01-15")
date2 <- as.Date("2024-02-15")
date2 - date1
difftime(date2, date1, units = "days")
date1 + 7
date1 - 10
seq(as.Date("2024-01-01"), as.Date("2024-01-10"), by = "day")
seq(as.Date("2024-01-01"), as.Date("2024-03-01"), by = "month")
2.5 日期组件 #
r
date <- as.Date("2024-01-15")
as.numeric(format(date, "%Y"))
as.numeric(format(date, "%m"))
as.numeric(format(date, "%d"))
weekdays(date)
months(date)
quarters(date)
julian(date)
三、POSIXct类型 #
3.1 创建日期时间 #
r
now <- Sys.time()
print(now)
class(now)
time1 <- as.POSIXct("2024-01-15 10:30:00")
print(time1)
time2 <- as.POSIXct("2024-01-15 10:30:00", format = "%Y-%m-%d %H:%M:%S")
print(time2)
3.2 时区处理 #
r
time <- as.POSIXct("2024-01-15 10:30:00", tz = "UTC")
print(time)
time_beijing <- as.POSIXct("2024-01-15 18:30:00", tz = "Asia/Shanghai")
print(time_beijing)
attr(time_beijing, "tzone") <- "UTC"
print(time_beijing)
3.3 格式化日期时间 #
r
time <- as.POSIXct("2024-01-15 10:30:45")
format(time, "%Y-%m-%d %H:%M:%S")
format(time, "%Y年%m月%d日 %H时%M分%S秒")
format(time, "%I:%M %p")
format(time, "%A, %B %d, %Y")
3.4 时间格式符号 #
| 符号 | 说明 | 示例 |
|---|---|---|
| %H | 小时(24小时制) | 00-23 |
| %I | 小时(12小时制) | 01-12 |
| %M | 分钟 | 00-59 |
| %S | 秒 | 00-59 |
| %p | AM/PM | AM/PM |
| %Z | 时区 | UTC |
3.5 时间计算 #
r
time1 <- as.POSIXct("2024-01-15 10:00:00")
time2 <- as.POSIXct("2024-01-15 12:30:00")
time2 - time1
difftime(time2, time1, units = "hours")
time1 + 3600
time1 + as.difftime(1, units = "hours")
time1 + as.difftime(30, units = "mins")
四、POSIXlt类型 #
4.1 创建POSIXlt #
r
time <- as.POSIXlt("2024-01-15 10:30:45")
print(time)
class(time)
4.2 访问组件 #
r
time <- as.POSIXlt("2024-01-15 10:30:45")
time$year + 1900
time$mon + 1
time$mday
time$hour
time$min
time$sec
time$wday
time$yday
4.3 POSIXct vs POSIXlt #
r
ct <- as.POSIXct("2024-01-15 10:30:00")
lt <- as.POSIXlt("2024-01-15 10:30:00")
class(ct)
class(lt)
as.numeric(ct)
unclass(lt)
五、日期时间转换 #
5.1 字符串转日期 #
r
as.Date("2024-01-15")
as.Date("2024/01/15", format = "%Y/%m/%d")
as.Date("January 15, 2024", format = "%B %d, %Y")
as.Date("15-Jan-2024", format = "%d-%b-%Y")
5.2 数值转日期 #
r
as.Date(0, origin = "1970-01-01")
as.Date(19000, origin = "1970-01-01")
5.3 日期时间互转 #
r
datetime <- as.POSIXct("2024-01-15 10:30:00")
as.Date(datetime)
date <- as.Date("2024-01-15")
as.POSIXct(date)
as.POSIXct(date, tz = "UTC")
六、常用函数 #
6.1 当前日期时间 #
r
Sys.Date()
Sys.time()
date()
6.2 日期检查 #
r
x <- as.Date("2024-01-15")
is.Date(x)
class(x) == "Date"
6.3 日期范围 #
r
dates <- c(as.Date("2024-01-01"), as.Date("2024-01-15"), as.Date("2024-01-10"))
min(dates)
max(dates)
range(dates)
6.4 日期序列 #
r
seq(as.Date("2024-01-01"), as.Date("2024-01-07"), by = "day")
seq(as.Date("2024-01-01"), by = "day", length.out = 7)
seq(as.Date("2024-01-01"), as.Date("2024-12-31"), by = "month")
seq(as.Date("2024-01-01"), as.Date("2024-12-31"), by = "quarter")
七、lubridate包 #
7.1 安装和加载 #
r
install.packages("lubridate")
library(lubridate)
7.2 解析日期 #
r
library(lubridate)
ymd("2024-01-15")
ymd("20240115")
ymd("2024/01/15")
dmy("15-01-2024")
mdy("01-15-2024")
ymd_hms("2024-01-15 10:30:45")
ymd_hm("2024-01-15 10:30")
7.3 提取组件 #
r
time <- ymd_hms("2024-01-15 10:30:45")
year(time)
month(time)
day(time)
hour(time)
minute(time)
second(time)
wday(time)
wday(time, label = TRUE)
yday(time)
7.4 时间计算 #
r
time <- ymd_hms("2024-01-15 10:30:45")
time + days(1)
time + hours(2)
time + minutes(30)
time + weeks(1)
time + months(2)
time + years(1)
7.5 时间间隔 #
r
start <- ymd_hms("2024-01-15 10:00:00")
end <- ymd_hms("2024-01-16 12:30:00")
interval(start, end)
duration <- end - start
as.duration(duration)
time_length(duration, "hours")
time_length(duration, "minutes")
7.6 向下取整 #
r
time <- ymd_hms("2024-01-15 10:30:45")
floor_date(time, "hour")
floor_date(time, "day")
floor_date(time, "month")
ceiling_date(time, "hour")
round_date(time, "hour")
八、实践示例 #
8.1 计算年龄 #
r
birth_date <- as.Date("1990-05-15")
today <- Sys.Date()
age <- as.numeric(difftime(today, birth_date, units = "days")) / 365.25
floor(age)
age_years <- as.numeric(format(today, "%Y")) - as.numeric(format(birth_date, "%Y"))
if (format(today, "%m-%d") < format(birth_date, "%m-%d")) {
age_years <- age_years - 1
}
print(age_years)
8.2 工作日计算 #
r
count_workdays <- function(start, end) {
dates <- seq(start, end, by = "day")
sum(!weekdays(dates) %in% c("Saturday", "Sunday"))
}
count_workdays(as.Date("2024-01-01"), as.Date("2024-01-31"))
8.3 时间序列分析 #
r
dates <- seq(as.Date("2024-01-01"), as.Date("2024-12-31"), by = "month")
values <- rnorm(12, mean = 100, sd = 10)
ts_data <- data.frame(date = dates, value = values)
print(ts_data)
九、总结 #
本章学习了:
- Date类型的创建和操作
- POSIXct和POSIXlt日期时间类型
- 日期时间格式化和解析
- 日期时间计算和比较
- 时区处理
- lubridate包的使用
日期时间处理是数据分析的重要技能,掌握这些函数可以高效处理时间序列数据!
最后更新:2026-03-27