Calculate Time Difference between Dates in R Programming – difftime() Function

difftime() function in R Language is used to calculate time difference between dates in the required units like days, weeks, months, years, etc.

Syntax: difftime(date1, date2, units)

Parameters:
date1, date2: Dates to calculate difference
units: Days, weeks, months, etc.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to find time difference
  
# Calling difftime() function
difftime("2020-5-16", "2020-1-15", units = "days")
difftime("2020-5-16", "2020-1-15", units = "weeks")

chevron_right


Output:

Time difference of 122 days
Time difference of 17.42857 weeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to find time difference
  
# Calling difftime() function
difftime("2020-5-16", "2020-1-15", units = "hours")
difftime("2020-5-16", "2020-1-15", units = "mins")

chevron_right


Output:

Time difference of 2928 hours
Time difference of 175680 mins
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.