3

Is there a function in R that calculates the number of days between two dates for any day-count basis? I am looking for something similar to the Matlab's daysdif function.

http://www.mathworks.com/help/finance/daysdif.html

In particular, I am interested in counting the number of days on the 360-day year, the equivalent of Excel's days360 function.

http://www.techonthenet.com/excel/formulas/days360.php

Is there a package that contains any helpful functions?

Kamil Kosiński
  • 107
  • 1
  • 6

1 Answers1

1

Does that suit you?

> as.POSIXct("2015-09-08")-as.POSIXct("2015-09-06")
Time difference of 2 days
> as.numeric(as.POSIXct("2015-09-08")-as.POSIXct("2015-09-06"))
[1] 2

EDIT

With lubridate package one may create such function

> library(lubridate)
> 
> FACTOR_30_360 <- function(date1, date2){
+    stopifnot(is.POSIXct(date1) | is.POSIXct(date2))
+    
+    (360*(year(date2)-year(date1)) + 30*(month(date2) - month(date1))
+     + day(date2)-day(date1))/360
+ }
> 
> FACTOR_30_360(as.POSIXct("2015-09-08"),
+               as.POSIXct("2015-09-10"))
[1] 0.005555556

EDIT2

Or try this package RQuantLib - page10

Marcin
  • 7,834
  • 8
  • 52
  • 99