How to get month name as output from date object?

Viewed 1856
d <- "2021-05-21"
lubridate::month(d)

That gives 5 in output, I want May to be displayed there. Any guesses?

4 Answers

We could use lubridate package: The trick here is to set the argument label of the function monthto TRUE:

library(lubridate)
ymd(d)
month(d, label=TRUE)

We can use base R

  format(as.Date(d), '%b') 

Using strftime().

strftime("2021-05-21", '%b')
# [1] "May"

The given example isn't a date object, so you can extract the month from the string

month.name[as.numeric(strsplit(d,"-")[[1]][2])]

If it is a Date class

format(d,"%B")
Related