R Advanced

How to Use Date Formats in R

To format dates in R, use the format() function with various specifications. Having your dates in the proper format allows you to know that they are dates and which calculations should be performed on them.

Date formats

Conversion specification Description Example
%a Abbreviated weekday Sun, Mon
%A Full weekday Sunday, Monday
%b or %h Abbreviated month Sep, Nov
%B Full month September, November
%d Day of the month
01-31
10, 20
%j Day of the year
001-366
250, 310
%m Month
01-12
09, 11
%U Week
01-53
with Sunday as the first day of the week
35, 45
%w Weekday
0-6
Sunday is 0
0, 4
%W Week
00-53
with Monday as the first day of the week
21, 27
%x Date, locale-specific
%y Year without century
00-99
84, 05
%Y Year with century
on input:
00 to 68 prefixed by 20
69 to 99 prefixed by 19
1984, 2011
%C Century 19, 20
%D Date formatted %m/%d/%y 09/10/93, 11/20/93
%u Weekday
1-7
Monday is 1
7, 4

Getting a day

To get today’s date, use the Sys.Date() function.

To get the abbreviated day, pass the format “%a” to the format() function.

For a full day, pass the format argument to the “%A”.

For a weekday, pass the format argument to the “%u”.

# today date
today_date <- Sys.Date()

# Abbreviated Day
format(today_date, format = "%a")

# Full Day
format(today_date, format = "%A")

# Weekday
format(today_date, format = "%u")

# Output
# [1] "Sun"
# [1] "Sunday"
# [1] "0"

Getting a month

To extract the day from the date, pass format = “%d” to the format() function.

For the month number, pass the format argument to the “%m”.

For an abbreviated month, pass the format argument to the “%b”.

For getting full month name from the date, pass the format argument to the “%B”.

today_date <- Sys.Date()

# Default format yyyy-mm-dd
today_date

# Day
format(today_date, format = "%d")

# Month
format(today_date, format = "%m")

# Abbreviated month
format(today_date, format = "%b")

# Full month
format(today_date, format = "%B")


# Output:
# [1] "2023-12-24"
# [1] "24"
# [1] "12"
# [1] "Dec"
# [1] "December"
# [1] "12/24/23"
# [1] "24-Dec-23"

Getting the year

To extract the year from the date, pass format = “%y” to the format() function.

To get the year with the century, pass the format argument to the “%Y”.

To get the only century, pass the format argument to the “%C”.

today_date <- Sys.Date()

# Only year
format(today_date, format = "%y")

# Year with Century
format(today_date, format = "%Y")

# Only Century
format(today_date, format = "%C")

# Output:
# [1] "23"
# [1] "2023"
# [1] "20"

That’s it!

Recent Posts

file.rename(): Renaming Single and Multiple Files in R

To rename a file in R, you can use the file.rename() function. It renames a…

15 hours ago

R prop.table() Function

The prop.table() function in R calculates the proportion or relative frequency of values in a…

20 hours ago

exp() Function: Calculate Exponential of a Number in R

The exp() is a built-in function that calculates the exponential of its input, raising Euler's…

21 hours ago

R split() Function: Splitting a Data

The split() function divides the input data into groups based on some criteria, typically specified…

1 week ago

colMeans(): Calculating the Mean of Columns in R Data Frame

The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…

2 weeks ago

rowMeans(): Calculating the Mean of rows of a Data Frame in R

The rowMeans() is a built-in, highly vectorized function in R that computes the arithmetic mean…

3 weeks ago