Change the format of a date · #31

This snippet needs the following libraries: base · How to load the right library →

To change the format of a date, use format().

format(var, "%d/%m/%Y")

Dive deeper

There is a lot of operators you can use to fine tune the format of a date. Here is below a list of common operators.

Let's consider the following date and time: 25 April 2025 at 19:52:07.

Date and time

  • %c: date and time formatted according to the current langage of your computer

Date

Full dates:

  • %x: date in numeric format according to the current langage of your computer (requires an input date formatted as %y/%m/%d)
  • %D: date in numeric US format, equivalent to %m/%d/%y (US format, 04/25/22)
  • %F: date in numeric format equivalent to %Y-%m-%d (2022-04-25)

Day:

  • %d: day of the month as decimal number (25, from 01 to 31)
  • %A: full name of the day in the current langage of your computer (Monday, Lundi, Montag…)
  • %a: abbreviated name of the day in the current langage of your computer (Mon, Lun…)

Month:

  • %m: month as decimal number (04, from 01 to 12)
  • %B: full name of the month in the current langage of the computer (April, Avril…)
  • %b (or %h) : abbreviated name of the month in the current langage of the computer (Apr, Avr…)

Year:

  • %y: year without century (22, from 00 to 99)
  • %Y: year with century (2022)

Time

Full time:

  • %X: time formatted according to the current langage of your computer (requires an input time formatted as %H:%M:%S)
  • %r: 12 hours format with AM/PM and seconds, equivalent to %I:%M:%S %p (07:52:07 PM)
  • %T: 24 hours format with seconds, equivalent to %H:%M:%S (19:52:07)
  • %R: 24 hours format without seconds, equivalent to %H:%M (19:52)

Hours:

  • %H: hours as decimal numbers in 24 hours format (19, from 00 to 23)
  • %I: hours as decimal numbers in 12 hours format (7, from 01 to 12)
  • %p: AM/PM. To use with %I only (PM)

Minutes:

  • %M: minutes as decimal numbers (52, from 00 to 59)

Seconds:

  • %S: seconds as integer (07, from 01 to 60)

Timezone:

  • %z: offset in hours and minutes from UTC, in four digits (+0200)
  • %Z: time zone abbreviation as a character string (CEST)

Documentation

R: Date-time Conversion Functions to and from Character

Access ready-to-use R code snippets

Unlock now