I have a task where I need to calculate
I take my data from a table that contains periods like this:
I need to know for a given month
Given month is the beginning of the current month:
To get the length I'm using this formula
if (enddt < month)
length = startdt.diffInDays(enddt)
else
if (startdt < month)
length = startdt.diffInDays(month)
In the range
if (monthOf(startdt) <= monthOf(month) && monthOf(enddt) >= monthOf(month))
active = true
To dertermine if is the start
if (monthOf(startdt) === monthOf(month) && yearOf(startdt) === yearOf(month))
start = true
And finally if last day was in previous month
if (monthOf(enddt) === monthOf(lastMonth(month)) && yearOf(lastMonth(month)) > 2016) // everything happens in 2017
end = true
Everything seems fine, however when I try to sum the numbers they don't add up. If I take active - end it should equal the sum of start, right?
No One
code monkey
Unless you are intent on doing this without any additional libraries then I suggest you take a look at Moment.js (momentjs.com) it makes this kind of date manipulation a breeze. I include it as a baseline dependancy for pretty much every project where date of time is going to be involved.