Parsing Dates and Time – Part 3: Exercises

April 25, 2018

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)