获取年、月、日

基本类型 / 日期和时间

# A fixed moment: the output stays the same
moment <- as.POSIXct("2027-01-01 22:13:20",
                     tz = "UTC")

# POSIXlt: years count from 1900, months from 0
parts <- as.POSIXlt(moment)
year <- parts$year + 1900
month <- parts$mon + 1
day <- parts$mday
hour <- parts$hour
minute <- parts$min
second <- parts$sec

# Sunday is 0, Saturday is 6
weekday <- parts$wday

# Monday is 1, Sunday is 7
iso_weekday <- as.integer(format(moment, "%u"))

cat("moment =", format(moment), "\n")
cat("year =", year, "\n")
cat("month =", month, "\n")
cat("day =", day, "\n")
cat("hour =", hour, "\n")
cat("minute =", minute, "\n")
cat("second =", second, "\n")
cat("weekday =", weekday, "\n")
cat("iso_weekday =", iso_weekday, "\n")