javascript - 如何计算两个日期之间经过的 "quarters"的数量,其中季度不仅仅是一年中的一个季度,而是特定日期?

标签 javascript date if-statement logic calculator

我正在尝试使用Javascript制作一个“拖欠租金计算器”,所需的功能之一是可以选择计算每周、每月或每季度的租金。

如果租金按季度支付,则当日期超过某一天时,迟交的季度数将会增加。每年的季度日为 25/03、24/06、29/09 和 25/12。

当输入日期的月份大于季度日期的月份,但其日期小于季度的日期时,我目前的代码返回不正确的值。

即对于“第一次错过付款日期”24/06/2014 和日期 22/04/2015,这应该返回 4,但实际上返回 1。它应该返回 4,因为此时已经过去了 4 个季度日期 ( 2014年6月24日、2014年9月29日、2014年12月25日和2015年3月25日)。

这是我的代码:

function getNumberPeriods() {
        var years = (getNumberYears());
        var days = (getNumberDays());
        if ((getPeriodLength()) == "Weekly") {
            return ((days - (days % 7)) / 7);
        } else if ((getPeriodLength()) == "Monthly") {
            var months = ((((options.untilDate).getMonth()) + 1) - (((options.dueDate).getMonth()) + 1) + (12 * years));
            if (((options.untilDate).getDate()) < ((options.dueDate).getDate())) {
                months--;
            }
            return (months + 1);
        } else if ((getPeriodLength()) == "Quarterly") {
            if ((options.dueDate).getMonth() == 2) {
                if (((options.untilDate).getMonth() <= 5) && ((options.untilDate).getDate() < 24)) {
                    return (1+(years * 4));
                }
                else if (((options.untilDate).getMonth() <= 5) && ((options.untilDate).getDate() >= 24)) {
                    return (2+(years * 4));
                }
                else if (((options.untilDate).getMonth() <= 8) && ((options.untilDate).getDate() < 29)) {
                    return (2 + (years * 4));
                }
                else if (((options.untilDate).getMonth() <= 8) && ((options.untilDate).getDate() >= 29)) {
                    return (3 + (years * 4));
                }
                else if (((options.untilDate).getMonth() <= 11) && ((options.untilDate).getDate() < 25)) {
                    return (3 + (years * 4));
                }
                else if (((options.untilDate).getMonth() <= 11) && ((options.untilDate).getDate() >= 25)) {
                    return ((years * 4)+4);
                }
                else return (years * 4);
            }
            else if ((options.dueDate).getMonth() == 5) {
                if ((options.untilDate).getMonth() <= 2 && (options.untilDate).getDate() < 25) {
                    return (3 + (years * 4));
                }
                else if ((options.untilDate).getMonth() <= 2 && (options.untilDate).getDate() >= 25) {
                    return (4+(years * 4));
                }
                else if ((options.untilDate).getMonth() <= 8 && (options.untilDate).getDate() < 29) {
                    return ((years * 4)+1);
                }
                else if ((options.untilDate).getMonth() <= 8 && (options.untilDate).getDate() >= 29) {
                    return ((years * 4)+2);
                }
                else if ((options.untilDate).getMonth() <= 11 && (options.untilDate).getDate() < 25) {
                    return (2 + (years * 4));
                }
                else if ((options.untilDate).getMonth() <= 11 && (options.untilDate).getDate() >= 25) {
                    return (3 + (years * 4));
                }
                else return (years * 4);
            }
            else if ((options.dueDate).getMonth() == 8) {
                if ((options.untilDate).getMonth() <= 2 && (options.untilDate).getDate() < 25) {
                    return (2 + (years * 4));
                }
                if ((options.untilDate).getMonth() <= 2 && (options.untilDate).getDate() >= 25) {
                    return (3 + (years * 4));
                }
                else if ((options.untilDate).getMonth() <= 5 && (options.untilDate).getDate() < 24) {
                    return (3 + (years * 4));
                }
                else if ((options.untilDate).getMonth() <= 5 && (options.untilDate).getDate() >= 24) {
                    return (4+(years * 4));
                }
                else if ((options.untilDate).getMonth() <= 11 && (options.untilDate).getDate() < 25) {
                    return ((years * 4)+1);
                }
                else if ((options.untilDate).getMonth() <= 11 && (options.untilDate).getDate() >= 25) {
                    return ((years * 4)+2);
                }
                else return (years * 4);
            }
            else if ((options.dueDate).getMonth() == 11) {
                if ((options.untilDate).getMonth() <= 2 && (options.untilDate).getDate() < 25) {
                    return ((years * 4)+1);
                }
                else if ((options.untilDate).getMonth() <= 2 && (options.untilDate).getDate() >= 25) {
                    return ((years * 4)+2);
                }
                else if ((options.untilDate).getMonth() <= 5 && (options.untilDate).getDate() < 24) {
                    return (2 + (years * 4));
                }
                else if ((options.untilDate).getMonth() <= 5 && (options.untilDate).getDate() >= 24) {
                    return (3 + (years * 4));
                }
                else if ((options.untilDate).getMonth() <= 8 && (options.untilDate).getDate() < 29) {
                    return (3 + (years * 4));
                }
                else if ((options.untilDate).getMonth() <= 8 && (options.untilDate).getDate() >= 29) {
                    return ((years * 4)+4);
                }
                else return (years * 4);
            }
            else alert("not werkin");
        }
    }


    function getNumberDays() {
        return ((((options.untilDate)) - ((options.dueDate))) / (1000 * 60 * 60 * 24));
    }
    function getNumberYears() {
        var dueMonth = (options.dueDate).getMonth();
        var dueDay = (options.dueDate).getDate();
        var dueYear = (options.dueDate).getFullYear();
        var untilYear = (options.untilDate).getFullYear();
        var untilMonth = (options.untilDate).getMonth();
        var untilDay = (options.untilDate).getDate();
        var diffyears = untilYear - dueYear; 

        if (untilMonth < dueMonth - 1){
         diffyears--;
            }
        if (dueMonth - 1 == untilMonth && untilDay < dueDay){
        diffyears--;
            }
        return diffyears;

this是我的 JS fiddle 的链接(我知道 fiddle 的其他部分无法正常工作,但我还没有得到这些)。

任何帮助将不胜感激!

泽拉。

最佳答案

以下代码将已通过的季度计数为已通过

var quarters = [{'month':3, 'day':25}, {'month':6, 'day':24}, 
    {'month':9, 'day':29}, {'month':12, 'day':25}];

var year = options.dueDate.getFullYear();
var passed = 0; // this is the number of passed quarters
var quarterIndex = 0;
do {
    // month is 0 based
    var reference = new Date(
        year, 
        quarters[quarterIndex].month - 1, 
        quarters[quarterIndex].day);

    if( (reference >= options.dueDate) && (reference <= options.untilDate) ) {
        passed ++;
    }

    quarterIndex++;
    if(4 == quarterIndex) {
        quarterIndex = 0;
        year++;
    }
} while(reference < options.untilDate);

关于javascript - 如何计算两个日期之间经过的 "quarters"的数量,其中季度不仅仅是一年中的一个季度,而是特定日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31986533/

相关文章:

java - 为什么我的按钮不会自行禁用?

java 日期问题

javascript - 获取 30 天后的一天

Scala - 获取给定年份的所有月份和日期

java - 逻辑错误导致 if 语句不起作用

java - 对JAVA处理这个工作流程和引用情况的方式感到困惑。有人可以澄清一下吗?

javascript - Jest Enzyme setState of memoryRouter dependent

php - 是否有任何代码库可以验证/将博客评论转换为严格的 XHTML?

javascript - 在 Chrome 控制台中执行多条 javascript 行时出现问题

javascript - 为什么这个内部函数返回未定义?