Skip to content

Instantly share code, notes, and snippets.

@ararog ararog/helpers.js
Last active Dec 30, 2015

Embed
What would you like to do?
Javascript version of Days360 excel function.
function days360(initialDate, currentDate) {
var dateA = initialDate;
var dateB = currentDate;
var dayA = dateA.getDate();
var dayB = dateB.getDate();
if (lastDayOfFebruary(dateA) && lastDayOfFebruary(dateB))
dayB = 30;
if (dayA == 31 && lastDayOfFebruary(dateA))
dayA = 30;
if (dayA == 30 && dayB == 31)
dayB = 30;
days = (dateB.getFullYear() - dateA.getFullYear()) * 360 +
((dateB.getMonth() + 1) - (dateA.getMonth() + 1)) * 30 + (dayB - dayA);
return days;
}
function lastDayOfFebruary(date) {
lastDay = new Date(date.getFullYear(), 2, -1);
return date.getDate() == lastDay;
}
@GrafRaf

This comment has been minimized.

Copy link

commented Apr 17, 2015

Excel has little more complex algoritm
For example for initialDate = 31/10/2014 and currentDate = 1/11/2014 Excel Days360() return 1
your function return 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.