当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 difftime()用法及代码示例


difftime()R语言中的函数用于以所需的单位(如天、周、月、年等)计算日期之间的时差。

用法: difftime(date1, date2, units)

参数:
date1,date2:计算差异的日期
units:天、周、月等。

范例1:


# 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")

输出:

Time difference of 122 days
Time difference of 17.42857 weeks

范例2:


# 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")

输出:

Time difference of 2928 hours
Time difference of 175680 mins

相关用法


注:本文由纯净天空筛选整理自nidhi_biet大神的英文原创作品 Calculate Time Difference between Dates in R Programming – difftime() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。