# 固定的时刻:输出始终相同
moment <- as.POSIXct("2027-01-01 22:13:20",
tz = "UTC")
# %I 是 12 小时制,%p 是 AM/PM
short_style <- format(moment, "%m/%d/%y %I:%M %p")
# short_style = 01/01/27 10:13 PM
custom_style <- format(moment, "%Y-%m-%d")
# custom_style = 2027-01-01
# 不指定格式:日期、空格、时间
standard_style <- format(moment)
# standard_style = 2027-01-01 22:13:20
# ISO 8601 格式,日期与时间之间用 "T"
iso_style <- format(moment, "%Y-%m-%dT%H:%M:%S")
# iso_style = 2027-01-01T22:13:20
# 嵌入句子中,使用同样的格式代码
str_format <- paste("Today",
format(moment, "%d %B"))
# str_format = Today 01 January
cat("short_style =", short_style, "\n")
cat("custom_style =", custom_style, "\n")
cat("standard_style =", standard_style, "\n")
cat("iso_style =", iso_style, "\n")
cat("str_format =", str_format, "\n")