Extract Month from a Date in R: Methods, Output Types and Pitfalls
How to get the month from a date in R with base R or lubridate, what each method returns, and how locale, time zones and exports affect the result.
Getting the month out of a date in R takes one line, but the methods return different kinds of values. Some give a zero-padded string, some a plain integer, some a locale-dependent name and some an ordered factor. Which one fits depends on whether you need to calculate with the month, sort by it, display it or export it. This review explains what each approach changes and when the result stays reliable.
Is month extraction suitable for your use?
Pulling out the month works well for seasonal comparisons, grouping by calendar month and building readable labels for tables or charts. The main tradeoff is that the month alone drops the year and the day. January 2023 and January 2024 become the same value. If your analysis spans more than one year, a month number is usually not enough on its own.
Keep the original Date column and add the month as a derived column rather than replacing it. Use an integer month when you need to compute, filter or join. Use a month label only for display. When you need a year-month period rather than a seasonal bucket, use a combined form such as "2024-03" or the first day of the month.
What each method actually returns
The common mechanism is that R stores a Date as a number of days since 1970-01-01. Extracting the month reads that stored value and produces a new value. The original date is not modified. The new value's type is the important difference between methods.
| Method | Example result for 2024-03-15 | Type returned | Notes |
| format(d, "%m") | "03" | character | Zero-padded string; wrap in as.integer() for a number |
| format(d, "%B") | "March" (English locale) | character | Full month name; depends on locale |
| format(d, "%b") | "Mar" (English locale) | character | Abbreviated name; depends on locale |
| months(d) | "March" (English locale) | character | Base R; abbreviate = TRUE gives the short form |
| as.POSIXlt(d)$mon + 1 | 3 | integer | $mon counts from 0, so add 1 |
| lubridate::month(d) | 3 | numeric | Requires the lubridate package |
| lubridate::month(d, label = TRUE) | Mar | ordered factor | Sorts in calendar order; abbr = FALSE gives full names |
| data.table::month(d) | 3 | integer | Requires the data.table package |
Base R
d <- as.Date("2024-03-15")
format(d, "%m") # "03"
as.integer(format(d, "%m")) # 3
format(d, "%B") # "March" in an English locale
months(d) # "March" in an English locale
as.POSIXlt(d)$mon + 1 # 3
lubridate
library(lubridate)
d <- as.Date("2024-03-15")
month(d) # 3
month(d, label = TRUE) # Mar (ordered factor)
month(d, label = TRUE, abbr = FALSE) # March (ordered factor)
floor_date(d, "month") # "2024-03-01", keeps the year
Dates stored as text
When dates arrive as character strings, parse them into Date objects first. Without an explicit format, as.Date() only recognizes standard forms such as "2024-03-15" or "2024/03/15". Other strings either fail or produce a wrong value. Day-first and month-first strings like "03/04/2024" are ambiguous, so state the format rather than relying on a guess.
x <- c("15/03/2024", "02/11/2023")
d <- as.Date(x, format = "%d/%m/%Y")
format(d, "%m") # "03" "11"
# lubridate equivalent when the order is day-month-year
lubridate::month(lubridate::dmy(x)) # 3 11
Pitfalls that change the meaning of the result
- Lost year: month() and format(d, "%m") merge the same month across years. For period grouping, use format(d, "%Y-%m") or floor_date(d, "month").
- Alphabetical sorting: month names stored as character sort as April, August, December and so on. Use lubridate's ordered factor, or factor(x, levels = month.name) for English names.
- Zero-based counting: as.POSIXlt(d)$mon returns 0 for January, so forgetting the + 1 shifts every month.
- Time zones: for date-time (POSIXct) values, the month is computed in a time zone. A timestamp just after midnight UTC on the 1st can belong to the previous month in another zone. Set the tz argument explicitly when converting or formatting.
- Missing values: unparseable strings become NA, and every method passes the NA through. Check for NA after parsing rather than after extraction.
- Text versus numbers: format() output is character, so comparisons such as "10" < "9" follow text rules. Convert to integer before numeric comparisons.
Will the result stay readable after export or sharing?
Month names from format(), months() and lubridate labels come from the system locale. The same script can print "March" on one machine and a name in another language elsewhere. On a Russian-language system, for example, the names come out in Russian, and the exact word form can vary by platform. If labels must be consistent, set the locale explicitly. lubridate's month() accepts a locale argument. The built-in constants month.name and month.abb are always English and do not change with locale.
# Locale-independent English labels
month.name[as.integer(format(d, "%m"))] # "March"
month.abb[as.integer(format(d, "%m"))] # "Mar"
Exporting also changes what readers receive. An ordered factor written to CSV becomes plain text labels, and reading the file back does not restore the calendar order. Zero-padded strings such as "03" may be reinterpreted as the number 3 by spreadsheet software or by an import step. When the file will be edited by people or reloaded by other tools, an integer month column paired with a year column is the most predictable choice. You can add a separate label column only for presentation.
Gaps in this review: no specific R version, spreadsheet application or operating system was tested here. Locale output and import behavior can differ between environments. Confirm the result in the environment where the data will actually be used.