How long has it been?
To get finer control over a difference between datetimes use the base
function difftime()
. For example instead of time1 - time2
, you use difftime(time1, time2)
.
difftime()
takes an argument units
which specifies the units for the difference. Your options are "secs"
, "mins"
, "hours"
, "days"
, or "weeks"
.
To practice you'll find the time since the first man stepped on the moon. You'll also see the lubridate
functions today()
and now()
which when called with no arguments return the current date and time in your system's timezone.
This exercise is part of the course
Working with Dates and Times in R
Exercise instructions
- Apollo 11 landed on July 20, 1969. Use
difftime()
to find the number of days betweentoday()
anddate_landing
. - Neil Armstrong stepped onto the surface at 02:56:15 UTC. Use
difftime()
to find the number of seconds betweennow()
andmoment_step
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# The date of landing and moment of step
date_landing <- mdy("July 20, 1969")
moment_step <- mdy_hms("July 20, 1969, 02:56:15", tz = "UTC")
# How many days since the first man on the moon?
difftime(___, ___, units = ___)
# How many seconds since the first man on the moon?
___