R language date format
How date formatting works in R with format(), lubridate and sprintf, what it changes in your data, and when a formatted date stays readable and usable after export.
Formatting a date in R is a text transformation. A Date or date-time object becomes a character string laid out for a particular reader, report or file. This review explains what that conversion changes, how base R, lubridate and sprintf fit together, and when the result stays meaningful once it leaves your R session. It covers the common format codes and the decisions that matter for localized output. It does not cover every option or platform.
Is formatting dates as text the right move?
The main benefit is control over how a date looks: a day-month-year order for European readers, a spelled-out month for a printed report, or an ISO 8601 string for a file another system will read. The main cost is that the result is no longer a date. Once a Date is formatted, R treats it as ordinary text. Date arithmetic, correct chronological sorting and time-zone handling stop working unless you parse the text back.
Format as late as possible. Keep dates as Date or POSIXct objects while you compute, filter and sort, and convert to text only for display or export. If the output will be read by software rather than people, prefer the unambiguous year-month-day form (for example 2026-10-31) over a localized style.
What formatting actually changes
Internally, a Date in R is a number counting days since 1970-01-01, with a class attribute that tells R to print it as a calendar date. POSIXct date-times work the same way but count seconds and carry a time zone. When you print a Date, R shows it in its default year-month-day layout, but the underlying value is unchanged. Calling format() is different. It returns a character vector, so the stored type changes, not just its appearance.
The term "date format" refers to two separate operations in R, and they are easy to confuse:
- Parsing: turning text into a date, for example as.Date("31.10.2026", format = "%d.%m.%Y"). The format string describes how the input is laid out.
- Formatting: turning a date into text, for example format(d, "%d.%m.%Y"). The format string describes the output you want.
- Both use the same percent-sign conversion codes, documented under ?strptime.
The examples below use a fixed date so the output is predictable. Results that include month or weekday names depend on the session's time locale. The names shown are what an English locale produces.
d <- as.Date("2026-10-31")
format(d, "%Y-%m-%d") # "2026-10-31" ISO 8601
format(d, "%d.%m.%Y") # "31.10.2026" day.month.year
format(d, "%m/%d/%Y") # "10/31/2026" US order
format(d, "%j") # "304" day of the year
format(d, "%B %d, %Y") # "October 31, 2026" in an English locale
format(d, "%A") # "Saturday" in an English locale
class(format(d, "%d.%m.%Y")) # "character"
| Code | Meaning |
| %Y | Four-digit year |
| %y | Two-digit year |
| %m | Month as a zero-padded number |
| %d | Day of month, zero-padded |
| %B | Full month name (locale-dependent) |
| %b | Abbreviated month name (locale-dependent) |
| %A | Full weekday name (locale-dependent) |
| %a | Abbreviated weekday name (locale-dependent) |
| %j | Day of year, 001 to 366 |
| %H:%M:%S | Hours, minutes, seconds (date-times) |
| %Z | Time zone abbreviation (date-times) |
Some codes, such as %e for a day without a leading zero, are passed through to the operating system's date library. Support for them may differ between platforms. If a script must run on several systems, test any code that is not listed in R's own documentation on each of them.
Where lubridate fits
lubridate makes parsing easier. It does not replace format(). Functions such as ymd(), dmy() and mdy() read text in a known component order without you writing a format string, and they return ordinary Date or POSIXct objects. For output, you still normally use base format() on the result.
library(lubridate)
d <- dmy("31.10.2026") # parse day-month-year text into a Date
format(d, "%Y-%m-%d") # "2026-10-31"
# stamp() builds a formatter from an example string
fmt <- stamp("March 1, 1999")
fmt(d) # formatted to resemble the example
stamp() guesses the layout from your example. If the example is ambiguous, for instance a date like 01/02/1999 where day and month could swap, it may pick a different order than you intended. Check its output on a date where the day is greater than 12 before relying on it.
Using sprintf with dates
sprintf() has no date conversion codes of its own. It is useful for placing an already formatted date into a larger string, such as a file name or a log line. The dependable pattern is to call format() first and pass the result to sprintf() with %s. That way the layout is stated explicitly. You avoid relying on how sprintf() converts a Date object by itself, which this review has not verified across R versions.
d <- as.Date("2026-10-31")
sprintf("report_%s.csv", format(d, "%Y%m%d"))
# "report_20261031.csv"
sprintf("Data as of %s", format(d, "%d.%m.%Y"))
# "Data as of 31.10.2026"
Compact year-month-day stamps such as 20261031 work well in file names because they sort chronologically as plain text.
Localized month and weekday names
The codes %B, %b, %A and %a take their names from the session's LC_TIME locale. The same script can produce English names on one machine and another language on a second. You can check the current setting with Sys.getlocale("LC_TIME") and change it with Sys.setlocale("LC_TIME", ...). The locale names that are accepted depend on the operating system, though. A name that works on Linux may fail on Windows, and the reverse.
Some languages, Russian among them, write the month name in a different grammatical form when it appears inside a full date than when it stands alone. Whether %B returns the form your sentence needs depends on the platform's locale data. This review has no evidence to say what a particular system returns. If grammatical correctness matters, test the output on the system that will produce the final document, or supply your own vector of month names and index it with the month number.
Will the formatted date stay usable after copying or sharing?
The output of format() is ordinary text. Numeric layouts use only digits and separators, and localized names use standard letters of that language. So copying and pasting does not damage the characters themselves. The risks come from meaning and from how the destination interprets the text:
- Ambiguity: 03/04/2026 means different dates to US and European readers. Spell out the month or use year-month-day when the audience is mixed or unknown.
- Sorting: day-first or month-first strings sort incorrectly as text. Year-month-day sorts correctly.
- Re-interpretation: spreadsheet programs and other tools may detect date-like text and convert it using their own regional settings. Check how your specific destination handles the exact format before relying on it.
- Round-tripping: to compute with the dates again, you must parse them with a matching format string or lubridate function. Localized month names can only be parsed in a locale that recognizes them.
- Time zones: a formatted date-time loses its time zone unless you include it, for example with %Z, or pass tz to format().
For accessibility, a spelled-out date such as 31 October 2026 is generally clearer to human readers than a purely numeric one. How a given screen reader announces numeric or ISO dates depends on the product and its settings. No such test was available for this review, so check with your target assistive technology if it matters. When one output must serve both people and software, a practical approach is to keep an ISO 8601 column for machines and add a separate human-readable column, or include the ISO form alongside the localized one.
Bottom line
Date formatting in R is reliable and well documented. format() and its strptime codes do the conversion, lubridate simplifies parsing, and sprintf() assembles strings once the date is already text. The tradeoff to weigh is that formatted output is text, not a date. Keep real date objects through your analysis. Use year-month-day for anything a machine will read or sort. Reserve localized month and weekday names for final human-facing output, and test that output on the system and in the destination where it will actually be used.