Working with Dates and Times Pt 4

rtip
datetime
Author

Steven P. Sanderson II, MPH

Published

May 17, 2023

Introduction

Formatting dates is an essential task in data analysis and programming. In R, there are various ways to manipulate and present dates according to specific requirements. In this blog post, we will explore the world of date formatting in R, uncovering the power of the strftime() function. We will walk through practical examples using the provided code snippet, demonstrating how to format dates in a clear and concise manner. So, let’s dive in and uncover the secrets of date formatting in R!

#Understanding the strftime() Function:

In R, the strftime() function allows us to format dates and times based on a set of predefined modifiers. These modifiers act as placeholders for different components of the date and time. By using these modifiers, we can customize the output format to suit our needs.

Let’s analyze the code snippet provided to gain a better understanding of the strftime() function and its capabilities.

# all of the modifiers
for (formatter in sort(c(letters, LETTERS))) {
  modifier <- paste0("%", formatter)
  print(
    paste0(
      modifier, 
      " used on: ",
      RightNow,
      " will give: ",
      strftime(RightNow, modifier)
    )
  )
}

The code snippet above iterates through a set of modifiers, both lowercase and uppercase letters, and applies each modifier to the RightNow variable. It then prints the modifier, the original RightNow value, and the formatted output. This allows us to see the effect of each modifier on the date and time representation.

Modifier Showcase:

Let’s explore some commonly used modifiers and their corresponding output formats:

%a - Abbreviated weekday name (e.g., "Mon").
%A - Full weekday name (e.g., "Monday").
%b - Abbreviated month name (e.g., "Jan").
%B - Full month name (e.g., "January").
%d - Day of the month (01-31).
%H - Hour in 24-hour format (00-23).
%I - Hour in 12-hour format (01-12).
%m - Month (01-12).
%M - Minute (00-59).
%p - AM/PM indicator.
%S - Second (00-59).
%Y - Year with century (e.g., "2023").
%y - Year without century (e.g., "23").

Feel free to experiment with different modifiers and observe the changes in the output format.

Here is a full example

RightNow <- Sys.time()

# all of the modifiers
for (formatter in sort(c(letters, LETTERS))) {
  modifier <- paste0("%", formatter)
  print(
    paste0(
      modifier, 
      " used on: ",
      RightNow,
      " will give: ",
      strftime(RightNow, modifier)
    )
  )
}
[1] "%a used on: 2023-05-17 09:34:47 will give: Wed"
[1] "%A used on: 2023-05-17 09:34:47 will give: Wednesday"
[1] "%b used on: 2023-05-17 09:34:47 will give: May"
[1] "%B used on: 2023-05-17 09:34:47 will give: May"
[1] "%c used on: 2023-05-17 09:34:47 will give: Wed May 17 09:34:47 2023"
[1] "%C used on: 2023-05-17 09:34:47 will give: 20"
[1] "%d used on: 2023-05-17 09:34:47 will give: 17"
[1] "%D used on: 2023-05-17 09:34:47 will give: 05/17/23"
[1] "%e used on: 2023-05-17 09:34:47 will give: 17"
[1] "%E used on: 2023-05-17 09:34:47 will give: E"
[1] "%f used on: 2023-05-17 09:34:47 will give: f"
[1] "%F used on: 2023-05-17 09:34:47 will give: 2023-05-17"
[1] "%g used on: 2023-05-17 09:34:47 will give: 23"
[1] "%G used on: 2023-05-17 09:34:47 will give: 2023"
[1] "%h used on: 2023-05-17 09:34:47 will give: May"
[1] "%H used on: 2023-05-17 09:34:47 will give: 09"
[1] "%i used on: 2023-05-17 09:34:47 will give: i"
[1] "%I used on: 2023-05-17 09:34:47 will give: 09"
[1] "%j used on: 2023-05-17 09:34:47 will give: 137"
[1] "%J used on: 2023-05-17 09:34:47 will give: J"
[1] "%k used on: 2023-05-17 09:34:47 will give:  9"
[1] "%K used on: 2023-05-17 09:34:47 will give: K"
[1] "%l used on: 2023-05-17 09:34:47 will give:  9"
[1] "%L used on: 2023-05-17 09:34:47 will give: L"
[1] "%m used on: 2023-05-17 09:34:47 will give: 05"
[1] "%M used on: 2023-05-17 09:34:47 will give: 34"
[1] "%n used on: 2023-05-17 09:34:47 will give: \n"
[1] "%N used on: 2023-05-17 09:34:47 will give: N"
[1] "%o used on: 2023-05-17 09:34:47 will give: o"
[1] "%O used on: 2023-05-17 09:34:47 will give: O"
[1] "%p used on: 2023-05-17 09:34:47 will give: AM"
[1] "%P used on: 2023-05-17 09:34:47 will give: am"
[1] "%q used on: 2023-05-17 09:34:47 will give: q"
[1] "%Q used on: 2023-05-17 09:34:47 will give: Q"
[1] "%r used on: 2023-05-17 09:34:47 will give: 09:34:47 AM"
[1] "%R used on: 2023-05-17 09:34:47 will give: 09:34"
[1] "%s used on: 2023-05-17 09:34:47 will give: 1684330487"
[1] "%S used on: 2023-05-17 09:34:47 will give: 47"
[1] "%t used on: 2023-05-17 09:34:47 will give: \t"
[1] "%T used on: 2023-05-17 09:34:47 will give: 09:34:47"
[1] "%u used on: 2023-05-17 09:34:47 will give: 3"
[1] "%U used on: 2023-05-17 09:34:47 will give: 20"
[1] "%v used on: 2023-05-17 09:34:47 will give: 17-May-2023"
[1] "%V used on: 2023-05-17 09:34:47 will give: 20"
[1] "%w used on: 2023-05-17 09:34:47 will give: 3"
[1] "%W used on: 2023-05-17 09:34:47 will give: 20"
[1] "%x used on: 2023-05-17 09:34:47 will give: 5/17/2023"
[1] "%X used on: 2023-05-17 09:34:47 will give: 9:34:47 AM"
[1] "%y used on: 2023-05-17 09:34:47 will give: 23"
[1] "%Y used on: 2023-05-17 09:34:47 will give: 2023"
[1] "%z used on: 2023-05-17 09:34:47 will give: -0400"
[1] "%Z used on: 2023-05-17 09:34:47 will give: EDT"

Conclusion

In this blog post, we explored the strftime() function in R, which provides powerful capabilities for formatting dates. By using the various modifiers available, we can easily customize the representation of dates and times to meet our specific requirements. Understanding date formatting is crucial for effective data analysis, visualization, and reporting.

Remember to refer to the R documentation for strftime() to discover additional modifiers and advanced formatting options. With the knowledge gained from this blog post, you are now equipped to master date formatting in R and handle dates with confidence in your programming endeavors.

Happy coding with R and may your dates always be formatted to perfection!