如何使用 Lubridate 将日期格式化为 "YYYY-Mon"?

How to format a Date as "YYYY-Mon" with Lubridate?

我想按照本帖 (Create a Vector of All Days Between Two Dates) 中的描述,创建两个指定时刻之间的日期矢量,步长为 1 个月,然后将其转换为数据可视化的因素。

但是,我想要 YYYY-Mon 中的日期,即。 2010 年 2 月,格式。但到目前为止,我只设法使用标准格式 2010-02-01 的日期,使用如下代码:

require(lubridate)
first <- ymd_hms("2010-02-07 15:00:00 UTC")
start <- ymd(floor_date(first, unit="month"))

last <- ymd_hms("2017-10-29 20:00:00 UTC")
end <- ymd(ceiling_date(last, unit="month"))

> start
[1] "2010-02-01"

> end
[1] "2017-11-01"

如何将格式更改为 YYYY-Mon?

您可以使用 format():

start %>% format('%Y-%b')

要创建向量,请使用 seq():

seq(start, end, by = 'month') %>% format('%Y-%b')

Obs:使用大写 'B' 作为完整的月份名称:'%Y-%B'