當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。