现金流量的日期范围

时间:2018-11-01 17:01:29

标签: javascript

我正在尝试创建带有日期的现金流量。 我有两个日期:开始和结束

如果按月付款,并且假设租金支付日为15日。那么现金流量将是: 10/15/2018 11/15/2018 2018年12月15日 1/15/2019 .....等等,直到结束日期。

类似地,如果每3个月支付一次租金,则现金流将如下所示: 10/15/2018 1/15/2018 4/15/2018 ...等等。

我有以下代码每次都有效,除非租金是在每月的第一天或每月的最后一天。

    function createLedger(stDate, etDate){
        if (stDate && etDate) {
            var d2 = new Date(etDate);
            var sDay = d2.getUTCDate();
            var sMonth = d2.getUTCMonth() + 1;
            var sYear = d2.getUTCFullYear();
            var endOfLeaseDate = sYear + "-" + sMonth + "-" + sDay;

            var d3 = new Date(stDate);
            var s1Day = d3.getUTCDate();
            var s1Month = d3.getUTCMonth() + 1;
            var s1Year = d3.getUTCFullYear();
            var startOfLeaseDate = s1Year + "-" + s1Month + "-" + s1Day;

            var ddlFrequency = document.getElementById("ddFrequency");
            var selectedFrequency = ddlFrequency.options[ddlFrequency.selectedIndex].value;
            if (selectedFrequency) {
                if (selectedFrequency == "D") {
                    dates = dateRange(startOfLeaseDate, endOfLeaseDate);
                }

问题出在这里:

                else if (selectedFrequency == "Q") {
                    dates = getQuartersDateRange(d3, d2)
                    dates = SortedQuarter(d3,dates);
                }

                else {
                    dates = [];
                }
            }
            else {
                dates = [];
            }
            createFormElement();
        }
    }

我有以下代码来获取日期范围和季度范围。

function getQuartersDateRange(startOfLeaseDate, endOfLeaseDate) {
        var dates = [];
        var qlist = listQuarters(startOfLeaseDate, endOfLeaseDate);
        for (var i = 0; i < qlist.length; i++) {
            var yearquarter = qlist[i].split('-');
            var dateQ = new Date(yearquarter[0], (yearquarter[1] * 3 - 3) + 1, startOfLeaseDate.getUTCDate());
            qDay = dateQ.getUTCDate();
            qMonth = dateQ.getUTCMonth();
            qYear = dateQ.getUTCFullYear();
            var qDate = qYear + "-" + qMonth + "-" + qDay;
            dates.push(qDate);
        }
        return dates;
    }

function SortedQuarter(startOfLeaseDate, dates) {
        var qdatesSorted = [];
        for (var j = 0; j < dates.length; j++) {
            var month;
            var splitDate = dates[j].split('-');
            if (j == 0)
                month = startOfLeaseDate.getUTCMonth() + 1;
            else {
                startOfLeaseDate.setMonth(startOfLeaseDate.getUTCMonth() + 3)
                month = startOfLeaseDate.getUTCMonth() + 1;
            }
            var qDate = splitDate[0] + "-" + month + "-" + splitDate[2];
            qdatesSorted.push(qDate);
        }
        return qdatesSorted;
    }
function listQuarters(sDate, eDate) {
        if (sDate > eDate) {
            var t = eDate;
            eDate = sDate;
            sDate = t;
        }
        sDate = new Date(sDate);
        sDate.setDate(2);

        var startQ = getQuarter(sDate);
        var endQ = getQuarter(eDate);
        var result = [startQ];
        while (startQ != endQ) {
            sDate.setMonth(sDate.getUTCMonth() + 3);
            startQ = getQuarter(sDate);
            result.push(startQ);
        }
        return result;
    }

这里的问题是,当开始日期= 2018年11月1日,结束日期= 01/31/2020 现金流打印如下 11/1/2018 3/1/2019 6/1/2019 9/1/2019 12/1/2019 ...依此类推。因此,它不会从11月1日到2018年2月1日,而是跳过月份并转到下一个月份。我不确定为什么只在月底或月初才这样做。

感谢您的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

使用时刻库​​为我工作。 aLSO,我将日期范围重写如下:

 function createLedger(stDate, etDate) {

      if (stDate && etDate) {
        var endOfLeaseDate = moment(etDate, "MM/DD/YYYY");
        var startOfLeaseDate = moment(stDate, "MM/DD/YYYY");
        dateRange(startOfLeaseDate, endOfLeaseDate);
      }
    }

    function dateRange(stDate, etDate) {
      var dates = [];

      var now = etDate.clone();
      var day = etDate.date();

      while(now.isAfter(stDate)) {
                            var month = now.clone().endOf("month");
                            if (now.date() < day && day <= month.date()) {
                                now.date(day);
                            }
                            dates.push(now.format("MM/DD/YYYY"));
                            //dates._reverse();
                            now = now.clone().subtract({"months": 1});
                        }
                        console.log(dates);
                    }

    function RunLedgerAndPV() {
      var stDate = "11/26/2018";
      var etDate = "09/25/2019";
      createLedger(stDate, etDate);
    }
    RunLedgerAndPV();