Topic

Dates and day counts

Interest accrues over time, and how much time lies between two dates is a convention. The same loan can cost a different amount under each.

date("2027-01-31").addMonths(1);                       // 2027-02-28
daysBetween("2027-01-01", "2027-07-01");               // 181
dayCount("2027-01-31", "2027-03-31", "30/360");        // { days: 60, yearFraction: 1/6 }
dayCount("2027-12-01", "2028-03-01", "actual/actual"); // { days: 91, yearFraction: 31/365 + 60/366 }

Dates

A CalendarDate is a year, a month and a day in the Gregorian calendar, with no time of day and no time zone, so it cannot shift by a day when a program runs somewhere else. Write one as "2027-03-15". addMonths keeps the day of the month where it can and otherwise uses the last day of the month; with { endOfMonth: true } a month-end stays a month-end (28 February goes to 31 March), which is the usual rule for coupon dates.

Conventions

namedays countedyeartypical use
30/360every month has 30 days (the US rule for the 31st)360corporate and municipal bonds, many mortgages
30E/360every month has 30 days (any 31st becomes a 30th)360Eurobonds
actual/360the real number360money markets
actual/365the real number365some loans and markets
actual/actualthe real number365 or 366 for the days in each yeargovernment bonds

Under actual/360 a full year of 365 days counts as 365/360365/360 of a year, so a rate quoted that way costs about 1.4% more than its face. The year fraction is an exact fraction in every case.

Dated cash flows use these: xnpv and xirr measure time from the first date, and a bond with a settlement date uses its convention for accrued interest.

callreturns
date(x)CalendarDate: year, month, day, addDays(n), addMonths(n, { endOfMonth }), compareTo, equals, isEndOfMonth
daysBetween(a, b)number
dayCount(from, to, convention){ days, yearFraction }
DAY_COUNTS, isLeap(year)the names; true or false
Try itEdit the code and run it with @ratmath/finance and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs