Last active
December 30, 2015 00:29
-
-
Save ararog/7749919 to your computer and use it in GitHub Desktop.
Javascript version of Days360 excel function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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