1. Learn
  2. /
  3. Courses
  4. /
  5. Working with Dates and Times in R

Exercise

Specifying an order with `parse_date_time()`

What about if you have something in a really weird order like dym_msh? There's no named function just for that order, but that is where parse_date_time() comes in. parse_date_time() takes an additional argument, orders, where you can specify the order of the components in the date.

For example, to parse "2010 September 20th" you could say parse_date_time("2010 September 20th", orders = "ymd") and that would be equivalent to using the ymd() function from the previous exercise.

One advantage of parse_date_time() is that you can use more format characters. For example, you can specify weekday names with A, I for 12 hour time, am/pm indicators with p and many others. You can see a whole list on the help page ?parse_date_time.

Another big advantage is that you can specify a vector of orders, and that allows parsing of dates where multiple formats might be used.

You'll try it out in this exercise.

Instructions

100 XP
  • x is a trickier datetime. Use the clues in the instructions to parse x.
  • two_orders has two different orders, parse both by specifying the order to be c("mdy", "dmy").
  • Parse short_dates with orders = c("dOmY", "OmY", "Y"). What happens to the dates that don't have months or days specified?