Parsing Dates and Time – Part 3: Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In the exercises below, we will continue our work with the lubridate package to see more features of it.

Answers to these exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

Exercise 1
Lubridate offers two functions to see the date and date-time, at the moment, in the system. Use them to see the time in your time zone.
Exercise 2

There is a base function named “difftime” to find time difference between two times. Now use this to find out the time difference in weeks between.
x <- "11 th april 2012"
y <- "April 24th 2018 11:59:59"

Exercise 3

Do the same as above and find the difference between the system time and x, in hours.

Exercise 4

Lets say we have:
x <- "11 th april 2018 11.30.00"

We want to add one day to x. How do we achieve that?

Exercise 5

Now add 100 hours to x and find which date-time it is.

Learn more about Data Pre-Processing in the online course R Data Pre-Processing & Data Management – Shape your Data!. In this course you will learn how to:

  • Work with different packages that help shape dates and times,
  • learn about the lubridate package for quick and easy dates and times,
  • and much more

Exercise 6

Now, how can we get the date-time of 3 weeks prior to x?

Exercise 7

Now, read about the duration and spans from lubridate’s vignette. Try getting the date-time 10 years prior to x first by subtracting with years and then by using 10 year duration’s. See if they are similar and try to reason it.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)