Date Formats in R

To format the dates in R, you can use the “format()” function based on different specifications. Having your dates in the proper format allows R to know that they are dates, and it knows what calculations it should and should not perform on them.

Date formats in R: Complete Table

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

Example 1:  Using the format() function on current date

To get the textual format of this date, use the “format()” function.

today <- Sys.Date()

format(today, format="%B %d %Y")

Output

[1] "March 17 2021"

Example 2: Getting abbreviated weekday from the date

today <- Sys.Date()

format(today, format="%a")

Output

[1] "Wed"

Example 3: Getting a full month from a Date

today <- Sys.Date()

format(today, format="%B")

Output

[1] "March"

Example 4: Getting the Day of the Month

today <- Sys.Date()

format(today, format="%d")

Output

[1] "17"

Example 5: How to get the Month from a date

today <- Sys.Date()

format(today, format="%m")

Output

[1] "03"

That’s it.

Leave a Comment