现金流量的日期逻辑

时间:2018-11-14 19:11:37

标签: javascript

我有3个日期:Present date:11/14/2018, Start of lease date: 11/8/2014End of lease dateremaining payments,即Present date的10笔付款。

当前日期始终是今天的日期。租赁开始可以是任何东西,但是如果start of leasepast中,我正在研究。

基于此,我有以下现金流量:

enter image description here

由于租约是从8月开始的,因此付款将在每月的8号开始。 我有以下代码,这些代码根据开始日期的时间来分析天数。

    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/>
                

这里的问题是我需要从今天开始计算end date,但要在start of lease date前1天结束。在这种情况下,它解析10 payments并且end date9/13/2019上。

但是,它应该在10/8/2019上。任何帮助表示赞赏。谢谢。

0 个答案:

没有答案