Skip to content

Instantly share code, notes, and snippets.

@ararog
Last active December 30, 2015 00:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ararog/7749919 to your computer and use it in GitHub Desktop.
Save ararog/7749919 to your computer and use it in GitHub Desktop.
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
Copy link

GrafRaf 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