Date logic for cashflows












0















I have 3 dates: Present date:11/14/2018, Start of lease date: 11/8/2014 and End of lease date
and the remaining payments which is 10 payments from Present date.



Present date is always today's date. Start of lease can be anything but I am working on if the start of lease was in the past.



Based on this i have the following cash flow:



enter image description here



Since the lease started on the 8th, the payments will be on 8th of every month.
I have the following code that parses number of days depending on when the start date is.






    function isLeapYear(year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
}
function getDaysInMonth(year, month) {
return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
function getCurrentDay(date) {
var dateOfToday;
if(date && date != "")
dateOfToday = new Date(date);
else dateOfToday = new Date();

var dd = ("0" + (dateOfToday.getDate())).slice(-2);
var mm = ("0" + (dateOfToday.getMonth() + 1)).slice(-2);
var yyyy = dateOfToday.getFullYear();

dateOfToday = yyyy + '-' + mm + '-' + dd;
return dateOfToday;
}
function PopulateEndDate() {
var d2;
var paymentDays;
var stDate;
var pDate;

if (document.getElementById("startDate"))
stDate = document.getElementById("startDate").value;
if (document.getElementById("presentDate"))
pDate = document.getElementById("presentDate").value;

var futureSDate = (pDate < stDate); //future start date
var pastSDate = (pDate > stDate); //start date was in the past
var sameDates = (pDate == stDate);

if (stDate && futureSDate)
d2 = new Date(stDate);
else if (stDate && sameDates)
d2 = new Date(stDate);
else if (stDate && pastSDate)
d2 = new Date(pDate);
else
d2 = new Date(pDate);

var dd = d2.getDate()+1000;
var mm = d2.getMonth()+1;
var yyyy = d2.getFullYear();
var today = (mm+'/'+dd+'/'+yyyy);


paymentDays = document.getElementById("paymentRemaining").value;

if (paymentDays)
var n = d2.getDate();
d2.setDate(1);
d2.setMonth(d2.getMonth() + parseInt(paymentDays));
d2.setDate(Math.min(n, getDaysInMonth(d2.getFullYear(), d2.getMonth())));
if (document.getElementById("endDate"))
document.getElementById("endDate").value = getCurrentDay(d2);
}

<label>Start of Lease </label> <input  id="startDate"  onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Present date </label> <input id="presentDate" onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Remaining Payments</label> <input id="paymentRemaining" name = "paymentRemaining" type="number" class="label1" style="text-align: right; width:80px;"> <br/><br/>
<label>End of Lease </label> <input id="endDate" onchange="PopulateEndDate()" type="date" ><br/><br/><br/>





The issue here is that I need the end date to be calculated from today's date however end 1 day before the start of lease date. In this case it parses 10 payments and the end date is on 9/13/2019.



But, it should be on 10/8/2019. Any help is appreciated. Thank you.










share|improve this question























  • Using a date library like moment.js would help you simplify the logic for something like this

    – charlietfl
    Nov 14 '18 at 19:15


















0















I have 3 dates: Present date:11/14/2018, Start of lease date: 11/8/2014 and End of lease date
and the remaining payments which is 10 payments from Present date.



Present date is always today's date. Start of lease can be anything but I am working on if the start of lease was in the past.



Based on this i have the following cash flow:



enter image description here



Since the lease started on the 8th, the payments will be on 8th of every month.
I have the following code that parses number of days depending on when the start date is.






    function isLeapYear(year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
}
function getDaysInMonth(year, month) {
return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
function getCurrentDay(date) {
var dateOfToday;
if(date && date != "")
dateOfToday = new Date(date);
else dateOfToday = new Date();

var dd = ("0" + (dateOfToday.getDate())).slice(-2);
var mm = ("0" + (dateOfToday.getMonth() + 1)).slice(-2);
var yyyy = dateOfToday.getFullYear();

dateOfToday = yyyy + '-' + mm + '-' + dd;
return dateOfToday;
}
function PopulateEndDate() {
var d2;
var paymentDays;
var stDate;
var pDate;

if (document.getElementById("startDate"))
stDate = document.getElementById("startDate").value;
if (document.getElementById("presentDate"))
pDate = document.getElementById("presentDate").value;

var futureSDate = (pDate < stDate); //future start date
var pastSDate = (pDate > stDate); //start date was in the past
var sameDates = (pDate == stDate);

if (stDate && futureSDate)
d2 = new Date(stDate);
else if (stDate && sameDates)
d2 = new Date(stDate);
else if (stDate && pastSDate)
d2 = new Date(pDate);
else
d2 = new Date(pDate);

var dd = d2.getDate()+1000;
var mm = d2.getMonth()+1;
var yyyy = d2.getFullYear();
var today = (mm+'/'+dd+'/'+yyyy);


paymentDays = document.getElementById("paymentRemaining").value;

if (paymentDays)
var n = d2.getDate();
d2.setDate(1);
d2.setMonth(d2.getMonth() + parseInt(paymentDays));
d2.setDate(Math.min(n, getDaysInMonth(d2.getFullYear(), d2.getMonth())));
if (document.getElementById("endDate"))
document.getElementById("endDate").value = getCurrentDay(d2);
}

<label>Start of Lease </label> <input  id="startDate"  onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Present date </label> <input id="presentDate" onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Remaining Payments</label> <input id="paymentRemaining" name = "paymentRemaining" type="number" class="label1" style="text-align: right; width:80px;"> <br/><br/>
<label>End of Lease </label> <input id="endDate" onchange="PopulateEndDate()" type="date" ><br/><br/><br/>





The issue here is that I need the end date to be calculated from today's date however end 1 day before the start of lease date. In this case it parses 10 payments and the end date is on 9/13/2019.



But, it should be on 10/8/2019. Any help is appreciated. Thank you.










share|improve this question























  • Using a date library like moment.js would help you simplify the logic for something like this

    – charlietfl
    Nov 14 '18 at 19:15
















0












0








0








I have 3 dates: Present date:11/14/2018, Start of lease date: 11/8/2014 and End of lease date
and the remaining payments which is 10 payments from Present date.



Present date is always today's date. Start of lease can be anything but I am working on if the start of lease was in the past.



Based on this i have the following cash flow:



enter image description here



Since the lease started on the 8th, the payments will be on 8th of every month.
I have the following code that parses number of days depending on when the start date is.






    function isLeapYear(year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
}
function getDaysInMonth(year, month) {
return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
function getCurrentDay(date) {
var dateOfToday;
if(date && date != "")
dateOfToday = new Date(date);
else dateOfToday = new Date();

var dd = ("0" + (dateOfToday.getDate())).slice(-2);
var mm = ("0" + (dateOfToday.getMonth() + 1)).slice(-2);
var yyyy = dateOfToday.getFullYear();

dateOfToday = yyyy + '-' + mm + '-' + dd;
return dateOfToday;
}
function PopulateEndDate() {
var d2;
var paymentDays;
var stDate;
var pDate;

if (document.getElementById("startDate"))
stDate = document.getElementById("startDate").value;
if (document.getElementById("presentDate"))
pDate = document.getElementById("presentDate").value;

var futureSDate = (pDate < stDate); //future start date
var pastSDate = (pDate > stDate); //start date was in the past
var sameDates = (pDate == stDate);

if (stDate && futureSDate)
d2 = new Date(stDate);
else if (stDate && sameDates)
d2 = new Date(stDate);
else if (stDate && pastSDate)
d2 = new Date(pDate);
else
d2 = new Date(pDate);

var dd = d2.getDate()+1000;
var mm = d2.getMonth()+1;
var yyyy = d2.getFullYear();
var today = (mm+'/'+dd+'/'+yyyy);


paymentDays = document.getElementById("paymentRemaining").value;

if (paymentDays)
var n = d2.getDate();
d2.setDate(1);
d2.setMonth(d2.getMonth() + parseInt(paymentDays));
d2.setDate(Math.min(n, getDaysInMonth(d2.getFullYear(), d2.getMonth())));
if (document.getElementById("endDate"))
document.getElementById("endDate").value = getCurrentDay(d2);
}

<label>Start of Lease </label> <input  id="startDate"  onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Present date </label> <input id="presentDate" onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Remaining Payments</label> <input id="paymentRemaining" name = "paymentRemaining" type="number" class="label1" style="text-align: right; width:80px;"> <br/><br/>
<label>End of Lease </label> <input id="endDate" onchange="PopulateEndDate()" type="date" ><br/><br/><br/>





The issue here is that I need the end date to be calculated from today's date however end 1 day before the start of lease date. In this case it parses 10 payments and the end date is on 9/13/2019.



But, it should be on 10/8/2019. Any help is appreciated. Thank you.










share|improve this question














I have 3 dates: Present date:11/14/2018, Start of lease date: 11/8/2014 and End of lease date
and the remaining payments which is 10 payments from Present date.



Present date is always today's date. Start of lease can be anything but I am working on if the start of lease was in the past.



Based on this i have the following cash flow:



enter image description here



Since the lease started on the 8th, the payments will be on 8th of every month.
I have the following code that parses number of days depending on when the start date is.






    function isLeapYear(year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
}
function getDaysInMonth(year, month) {
return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
function getCurrentDay(date) {
var dateOfToday;
if(date && date != "")
dateOfToday = new Date(date);
else dateOfToday = new Date();

var dd = ("0" + (dateOfToday.getDate())).slice(-2);
var mm = ("0" + (dateOfToday.getMonth() + 1)).slice(-2);
var yyyy = dateOfToday.getFullYear();

dateOfToday = yyyy + '-' + mm + '-' + dd;
return dateOfToday;
}
function PopulateEndDate() {
var d2;
var paymentDays;
var stDate;
var pDate;

if (document.getElementById("startDate"))
stDate = document.getElementById("startDate").value;
if (document.getElementById("presentDate"))
pDate = document.getElementById("presentDate").value;

var futureSDate = (pDate < stDate); //future start date
var pastSDate = (pDate > stDate); //start date was in the past
var sameDates = (pDate == stDate);

if (stDate && futureSDate)
d2 = new Date(stDate);
else if (stDate && sameDates)
d2 = new Date(stDate);
else if (stDate && pastSDate)
d2 = new Date(pDate);
else
d2 = new Date(pDate);

var dd = d2.getDate()+1000;
var mm = d2.getMonth()+1;
var yyyy = d2.getFullYear();
var today = (mm+'/'+dd+'/'+yyyy);


paymentDays = document.getElementById("paymentRemaining").value;

if (paymentDays)
var n = d2.getDate();
d2.setDate(1);
d2.setMonth(d2.getMonth() + parseInt(paymentDays));
d2.setDate(Math.min(n, getDaysInMonth(d2.getFullYear(), d2.getMonth())));
if (document.getElementById("endDate"))
document.getElementById("endDate").value = getCurrentDay(d2);
}

<label>Start of Lease </label> <input  id="startDate"  onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Present date </label> <input id="presentDate" onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Remaining Payments</label> <input id="paymentRemaining" name = "paymentRemaining" type="number" class="label1" style="text-align: right; width:80px;"> <br/><br/>
<label>End of Lease </label> <input id="endDate" onchange="PopulateEndDate()" type="date" ><br/><br/><br/>





The issue here is that I need the end date to be calculated from today's date however end 1 day before the start of lease date. In this case it parses 10 payments and the end date is on 9/13/2019.



But, it should be on 10/8/2019. Any help is appreciated. Thank you.






    function isLeapYear(year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
}
function getDaysInMonth(year, month) {
return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
function getCurrentDay(date) {
var dateOfToday;
if(date && date != "")
dateOfToday = new Date(date);
else dateOfToday = new Date();

var dd = ("0" + (dateOfToday.getDate())).slice(-2);
var mm = ("0" + (dateOfToday.getMonth() + 1)).slice(-2);
var yyyy = dateOfToday.getFullYear();

dateOfToday = yyyy + '-' + mm + '-' + dd;
return dateOfToday;
}
function PopulateEndDate() {
var d2;
var paymentDays;
var stDate;
var pDate;

if (document.getElementById("startDate"))
stDate = document.getElementById("startDate").value;
if (document.getElementById("presentDate"))
pDate = document.getElementById("presentDate").value;

var futureSDate = (pDate < stDate); //future start date
var pastSDate = (pDate > stDate); //start date was in the past
var sameDates = (pDate == stDate);

if (stDate && futureSDate)
d2 = new Date(stDate);
else if (stDate && sameDates)
d2 = new Date(stDate);
else if (stDate && pastSDate)
d2 = new Date(pDate);
else
d2 = new Date(pDate);

var dd = d2.getDate()+1000;
var mm = d2.getMonth()+1;
var yyyy = d2.getFullYear();
var today = (mm+'/'+dd+'/'+yyyy);


paymentDays = document.getElementById("paymentRemaining").value;

if (paymentDays)
var n = d2.getDate();
d2.setDate(1);
d2.setMonth(d2.getMonth() + parseInt(paymentDays));
d2.setDate(Math.min(n, getDaysInMonth(d2.getFullYear(), d2.getMonth())));
if (document.getElementById("endDate"))
document.getElementById("endDate").value = getCurrentDay(d2);
}

<label>Start of Lease </label> <input  id="startDate"  onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Present date </label> <input id="presentDate" onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Remaining Payments</label> <input id="paymentRemaining" name = "paymentRemaining" type="number" class="label1" style="text-align: right; width:80px;"> <br/><br/>
<label>End of Lease </label> <input id="endDate" onchange="PopulateEndDate()" type="date" ><br/><br/><br/>





    function isLeapYear(year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
}
function getDaysInMonth(year, month) {
return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
function getCurrentDay(date) {
var dateOfToday;
if(date && date != "")
dateOfToday = new Date(date);
else dateOfToday = new Date();

var dd = ("0" + (dateOfToday.getDate())).slice(-2);
var mm = ("0" + (dateOfToday.getMonth() + 1)).slice(-2);
var yyyy = dateOfToday.getFullYear();

dateOfToday = yyyy + '-' + mm + '-' + dd;
return dateOfToday;
}
function PopulateEndDate() {
var d2;
var paymentDays;
var stDate;
var pDate;

if (document.getElementById("startDate"))
stDate = document.getElementById("startDate").value;
if (document.getElementById("presentDate"))
pDate = document.getElementById("presentDate").value;

var futureSDate = (pDate < stDate); //future start date
var pastSDate = (pDate > stDate); //start date was in the past
var sameDates = (pDate == stDate);

if (stDate && futureSDate)
d2 = new Date(stDate);
else if (stDate && sameDates)
d2 = new Date(stDate);
else if (stDate && pastSDate)
d2 = new Date(pDate);
else
d2 = new Date(pDate);

var dd = d2.getDate()+1000;
var mm = d2.getMonth()+1;
var yyyy = d2.getFullYear();
var today = (mm+'/'+dd+'/'+yyyy);


paymentDays = document.getElementById("paymentRemaining").value;

if (paymentDays)
var n = d2.getDate();
d2.setDate(1);
d2.setMonth(d2.getMonth() + parseInt(paymentDays));
d2.setDate(Math.min(n, getDaysInMonth(d2.getFullYear(), d2.getMonth())));
if (document.getElementById("endDate"))
document.getElementById("endDate").value = getCurrentDay(d2);
}

<label>Start of Lease </label> <input  id="startDate"  onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Present date </label> <input id="presentDate" onchange="PopulateEndDate()" type="date" ><br/><br/>
<label>Remaining Payments</label> <input id="paymentRemaining" name = "paymentRemaining" type="number" class="label1" style="text-align: right; width:80px;"> <br/><br/>
<label>End of Lease </label> <input id="endDate" onchange="PopulateEndDate()" type="date" ><br/><br/><br/>






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 19:11









ace23ace23

5811




5811













  • Using a date library like moment.js would help you simplify the logic for something like this

    – charlietfl
    Nov 14 '18 at 19:15





















  • Using a date library like moment.js would help you simplify the logic for something like this

    – charlietfl
    Nov 14 '18 at 19:15



















Using a date library like moment.js would help you simplify the logic for something like this

– charlietfl
Nov 14 '18 at 19:15







Using a date library like moment.js would help you simplify the logic for something like this

– charlietfl
Nov 14 '18 at 19:15














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307230%2fdate-logic-for-cashflows%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307230%2fdate-logic-for-cashflows%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine