dash–calculator

04 Numbers and Counting

Remove Year from Date in R: Easy Ways

Readout 04-0411 · Updated · Standard formula, shown below

The calculator on this page is switched off while it is checked. The formula is below, and the worked example shows how to do the sum by hand.

To remove the year from a date in R, you can use the format() function with a format string that excludes the year, such as format(date, "%m-%d") or format(date, "%B %d"), which returns the month and day only. Alternatively, you can use the lubridate package’s month() and day() functions to extract the components separately. This article explains the various methods, their outputs, and common pitfalls.

Formula

The core operation in R is the format() function, which converts a Date or POSIXct object into a character string according to a specified format. The format string uses placeholders starting with %:

  • %Y – four-digit year (e.g., 2025)
  • %y – two-digit year (e.g., 25)
  • %m – month as a zero-padded number (01–12)
  • %B – full month name (e.g., March)
  • %b – abbreviated month name (e.g., Mar)
  • %d – day of the month as a zero-padded number (01–31)
  • %A – full weekday name (e.g., Saturday)

To remove the year, simply omit any year placeholder from the format string. For example, format(date, "%m/%d") yields “03/15”. The result is always a character string, not a Date object, because a Date without a year is not a complete date.

Worked Example

Using the default input from the calculator: date = “2025-03-15” and format = “Month-Day” (which uses %m-%d).

  1. Start with the Date object: as.Date("2025-03-15") → 2025-03-15.
  2. Apply format() with "%m-%d": format(as.Date("2025-03-15"), "%m-%d").
  3. The output is the character string "03-15".

This matches the calculator’s first output. If you choose “Month Day” (format "%B %d"), the result is "March 15".

Why Remove the Year?

There are several common reasons to strip the year from a date in R:

  • You want to compare dates across different years, such as birthdays or annual events, ignoring the year to focus on month and day.
  • You need to create a seasonal or monthly label for plotting, where the year is irrelevant (e.g., average temperature by month-day across multiple years).
  • You are formatting dates for a report or dashboard where the year is implied or shown elsewhere.
  • You want to group data by month and day only, for example, to analyze patterns that repeat annually.

In all these cases, the year is not needed for the analysis or display, so removing it simplifies the output.

Methods to Remove the Year

R offers several ways to achieve this, each with its own use case:

  • Using format(): The most straightforward method. It returns a character vector. Example: format(as.Date("2025-03-15"), "%m-%d") → "03-15".
  • Using lubridate package: Functions like month() and day() extract the numeric components. Example: library(lubridate); d <- ymd("2025-03-15"); paste(month(d), day(d), sep="-") → "3-15" (note: no leading zeros unless you use sprintf).
  • Using strftime(): This is an alias for format() and works identically. Example: strftime(as.Date("2025-03-15"), "%d %B") → "15 March".
  • Using substr() on a character string: If your date is already a character in "YYYY-MM-DD" format, you can extract characters 6 to 10. Example: substr("2025-03-15", 6, 10) → "03-15". This is quick but fragile if the format varies.

Each method returns a different type: format() and strftime() return character strings; lubridate returns numeric vectors for month and day separately. Choose based on whether you need the result for display or for further numeric operations.

How to Choose the Right Method

Consider the following factors when deciding which method to use:

  • Output type: If you need a single string for labeling, use format() or strftime(). If you need numeric month and day for calculations (e.g., to compute day-of-year), use lubridate.
  • Leading zeros: format() with %m and %d gives zero-padded numbers (e.g., "03-15"). lubridate gives integers without padding (e.g., 3 and 15). If you need padding, use sprintf("%02d", month(d)).
  • Locale: Month names from %B depend on the system locale. If you need consistent English names, set Sys.setlocale("LC_TIME", "C") before formatting.
  • Performance: For large vectors, format() is vectorized and fast. lubridate is also vectorized but may have overhead due to package loading.
  • Readability: Code using lubridate is often more readable to newcomers, especially the ymd() function for parsing.

In practice, format() is the most common and recommended approach for simple removal of the year.

Common Mistakes and Pitfalls

Here are typical errors users encounter:

  • Forgetting to convert to Date: If your variable is a character string, format() will treat it as a character and may return an error or unexpected output. Always ensure it is a Date or POSIXct object first.
  • Using %Y inadvertently: Double-check your format string. A common typo is using %y (two-digit year) when you meant to omit it entirely.
  • Misunderstanding the output type: The result is a character string, not a Date. If you later try to plot it as a date, it will fail. You may need to create a dummy year (e.g., 2000) to keep it as a Date.
  • Locale issues with month names: On non-English systems, %B may return localized month names. This can cause inconsistency if you share code.
  • Time zone with POSIXct: If your datetime is POSIXct, the date part can be affected by time zone. Use as.Date() to strip the time before formatting.

For example, if you have datetime <- as.POSIXct("2025-03-15 14:30:00", tz="UTC"), then format(datetime, "%m-%d") works, but if you want to ignore the time, first do as.Date(datetime).

Alternatives to Removing the Year

Sometimes you may not want to remove the year entirely but rather ignore it in analysis. Alternatives include:

  • Converting to a factor with month-day levels: Use format(date, "%m-%d") as a factor for grouping.
  • Using lubridate::yday(): This gives the day of the year (1–366), which inherently ignores the year but keeps a numeric scale for plotting.
  • Setting a common year: Replace the year with a fixed one (e.g., 2000) using update(date, year=2000) from lubridate, then keep it as a Date for plotting.
  • Using cut() on month-day: If you want to bin by month or day, you can extract the month and use it as a factor.

These approaches preserve the date-like nature for plotting or grouping while effectively ignoring the year.

Frequently Asked Questions

How do I remove the year from a column of dates in a data frame?

Use mutate() from dplyr: df %>% mutate(month_day = format(date, "%m-%d")). This adds a new character column with the month and day, leaving the original date column unchanged.

Can I remove the year and keep the result as a Date object?

No, because a Date requires a year. To keep it as a Date, you must assign a dummy year, e.g., as.Date(format(date, "2000-%m-%d")). This is useful for plotting across years on a single axis.

What is the difference between format() and strftime()?

They are identical in R; strftime() is an alias for format() when applied to date-time objects. You can use either interchangeably.

How do I get the month name and day without leading zeros?

Use format(date, "%B %e") where %e gives the day without leading zeros (space-padded). Alternatively, use lubridate::month(date, label=TRUE) and day(date) and paste them together.

Why does format() return a character instead of a number?

Because the output is meant for display, not calculation. If you need numeric month and day, use as.numeric(format(date, "%m")) or the lubridate functions.

Does removing the year affect time zone handling?

If your date-time is POSIXct, the time zone can shift the date. Always convert to Date first with as.Date() to avoid surprises, especially if you are working with UTC vs local times.

Related

Formula used
in d | Date (YYYY-MM-DD) | - | 2025-03-15 | 1900-01-01 | 2100-12-31
sel f | Format | Month-Day=1, Month Day=2, Month=3, Day=4 | 1
out res | Result | - | if(f==1, format(as.Date(d,"%Y-%m-%d"),"%m-%d"), if(f==2, format(as.Date(d,"%Y-%m-%d"),"%B %d"), if(f==3, format(as.Date(d,"%Y-%m-%d"),"%B"), format(as.Date(d,"%Y-%m-%d"),"%d")))) | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More on this panel

The whole Numbers and Counting panel →