Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:计算租赁剩余时间(然后计算租金)(租金计算器)_Javascript - Fatal编程技术网

Javascript:计算租赁剩余时间(然后计算租金)(租金计算器)

Javascript:计算租赁剩余时间(然后计算租金)(租金计算器),javascript,Javascript,我在一家公司房地产代理公司工作,我正在创建一个供员工使用的内部页面,其中只包含一些供员工使用的小工具 我一直在尝试组装一个租金计算器,它根据用户提供的租金金额和租赁到期日,需要确定租赁剩余时间(从今天开始),然后告知租赁剩余租金 大部分情况下,我都能让它正常工作,但正如你所看到的,它相当长(我认为在某种程度上它必须如此),我感觉相当混乱: //calculates the remaining rent left to pay for terminated leases $("input[name

我在一家公司房地产代理公司工作,我正在创建一个供员工使用的内部页面,其中只包含一些供员工使用的小工具

我一直在尝试组装一个租金计算器,它根据用户提供的租金金额和租赁到期日,需要确定租赁剩余时间(从今天开始),然后告知租赁剩余租金

大部分情况下,我都能让它正常工作,但正如你所看到的,它相当长(我认为在某种程度上它必须如此),我感觉相当混乱:

//calculates the remaining rent left to pay for terminated leases
$("input[name='calcRent']").click(function() {
    //gets rent and daily rent for later calculations
    var rent = $("input[name='rentRent']").val();
    var dailyRate = (rent * 12) / 365;
    var leaseExpiry = $("input[name='leaseExpiry']").val();
    var remRent = $("input[name='remRent']");
    //breaks down lease expiry date and today's date into day, month, year parts
    //so that units can be used in calculations
    var ldd = leaseExpiry.substr(0,2);
        ldd = parseInt(ldd, 10);
    var lmm = leaseExpiry.substr(3,2);
        lmm = parseInt(lmm, 10);
    var lyyyy = leaseExpiry.substr(6,4);
        lyyyy = parseInt(lyyyy, 10);
    var date = new Date();
    var tdd = date.getDate();
    var tmm = date.getMonth()+1;
    var tyyyy = date.getFullYear();
        //if the expiry month is next year (or later) add 12 to expiry
        //month value to make "lmm - tmm" calculation give positive value
        if (lyyyy > tyyyy) {
            lmm += (12 * (lyyyy - tyyyy));
        }
    //takes the current month from the expiry month to get the number of
    //whole months left in the lease, then checks day values to see whether
    //we have already passed the rent due date for this month (and so there's
    //one less whole month left than we originally thought), taking 1 from
    //wholeMths value if so
    var wholeMths = lmm - tmm;
        if (ldd == (tdd - 1)) {
            wholeMths = wholeMths;
        } else if (ldd < (tdd - 1)) {
            wholeMths -= 1;
        }
    //works out if there are any days to be charged at daily rate (i.e. not
    //part of a whole month). If today's date(tdd) == expiry date(ldd)+1 we have no
    //leftover days (rental month runs like so: 12/04 - 11/05). If tdd > ldd+1
    //(leftover days cross over a month end) we set checkMonth to true so the following
    //if statement runs
    var checkMonth = false;
    var daysLeft = 0;
        if (tdd == (ldd + 1)) {
            daysLeft = 0;
        } else if (tdd > ldd + 1) {
            daysLeft = (31 - tdd) + ldd;
            checkMonth = true;
        } else {
            daysLeft = ldd - tdd;
        }
        //as per the above line: "daysLeft = (31 - tdd) + ldd;" we assume months have 31 days
        //as the majority do, this if checks whether the month end that we cross over with our
        //leftover days actually has 30 days, if not we check whether it's February and whether
        //it's a leap year so that we get the appropriate no. of days to charge for - if it meets
        //any of these criteria the relevant no. of days are subtracted from daysLeft
        if ((lmm == 05 || lmm == 07 || lmm == 10 || lmm == 12
            || lmm == 17 || lmm == 19 || lmm == 22 || lmm == 24) && checkMonth) {
            daysLeft -= 1;
        } else if ((lmm == 03 || lmm == 15) && checkMonth) {
            if (lyyyy % 4 == 0) {
                daysLeft -= 2;
            } else {
                daysLeft -= 3;
            }
        }
        checkMonth = false;
    var balance = (wholeMths * rent) + (daysLeft * dailyRate);

    remRent.val(balance.toFixed(2));        
});
//计算剩余租金以支付终止的租约
$(“输入[name='calcRent']”)。单击(函数(){
//获取租金和日租金以供以后计算
var rent=$(“输入[name='rentRent']”)val();
var dailyRate=(租金*12)/365;
var leaseExpiry=$(“输入[name='leaseExpiry']”)val();
var remRent=$(“输入[name='remRent']”);
//将租约到期日和今天的日期分解为日、月、年
//这样就可以在计算中使用单位
var ldd=租赁探索子公司(0,2);
ldd=parseInt(ldd,10);
var lmm=租赁探索子公司(3,2);
lmm=parseInt(lmm,10);
var lyyyy=租赁探索子公司(6,4);
lyyyy=parseInt(lyyyy,10);
变量日期=新日期();
var tdd=date.getDate();
var tmm=date.getMonth()+1;
var tyyyy=date.getFullYear();
//如果到期月份为下一年(或更晚),则在到期日前加上12个月
//进行“lmm-tmm”计算的月值为正值
如果(lyyyy>tyyyy){
lmm+=(12*(lyyyy-tyyyy));
}
//从到期月份开始取当前月份以获取
//租约中剩余的整个月数,然后检查日值以查看
//我们已经过了本月的租金到期日(因此
//比我们原来想象的少了整整一个月),从
//如果是的话,谁有价值
var wholeMths=lmm-tmm;
如果(ldd==(tdd-1)){
整体=整体;
}否则如果(ldd<(tdd-1)){
整数-=1;
}
//确定是否有按日费率收费的天数(即没有
//如果今天的日期(tdd)=到期日(ldd)+1,我们没有
//剩余天数(租赁月的运行方式为:2004年12月-2005年11月)。如果tdd>ldd+1
//(剩余天数与月末交叉)我们将checkMonth设置为true,以便
//if语句运行
var checkMonth=false;
var daysLeft=0;
如果(tdd==(ldd+1)){
daysLeft=0;
}否则如果(tdd>ldd+1){
daysLeft=(31-tdd)+ldd;
checkMonth=true;
}否则{
daysLeft=ldd-tdd;
}
//根据上述行:“daysLeft=(31-tdd)+ldd”;我们假设月份有31天
//正如大多数人所做的那样,这个if检查我们是否在月底与我们的
//剩下的日子实际上有30天,如果不是,我们检查是否是二月,是否
//这是闰年,因此我们可以获得适当的天数来收费——如果符合条件的话
//这些标准中的任何一项都会从daysLeft中减去相关天数
如果((lmm==05 | | lmm==07 | | lmm==10 | | lmm==12
||lmm==17 | | lmm==19 | | lmm==22 | | | lmm==24)和支票月){
daysLeft-=1;
}否则如果((lmm==03 | | lmm==15)和检查月){
如果(lyyyy%4==0){
daysLeft-=2;
}否则{
daysLeft-=3;
}
}
checkMonth=false;
var余额=(总资产*租金)+(daysLeft*日利率);
剩余价值(余额为固定值(2));
});
其中一个特别困扰我的地方是,租约在接下来的一年到期。我还没有弄清楚如何处理当月的“价值”(正如你在最后的if中看到的)

如果您对此有任何建议,我们将不胜感激,因为评论的数量和数量似乎与实际代码有点不成比例——但我认为它们目前是必要的,因为现在还不太清楚到底发生了什么


谢谢,

在您所做的工作上做得很好,但是您需要利用JavaScript中使用
date
类提供的内置日期计算

具体而言,要获取从日期到日期之间的天数,可以使用以下逻辑:

var currentDate = new Date();    // This will get today's date
currentDate.setHours(0,0,0,0);   // Remove time from consideration
                                 // As recommended by @NickSlash

// The following get's the lease expiration date using the year,
// month and date that you have already extracted from the input
var leaseExpirationDate = new Date( lyyyy, lmm, ldd ); 

// The number of days remaining in the lease is simply the following:
var one_day = 1000*60*60*24;
var daysLeftInLease = (leaseExpirationDate - currentDate ) / one_day;

之所以会出现这种神奇现象,是因为在内部,
Date
类将日期值保留为自1970年1月1日以来的毫秒数。因此,如果减去两个
Date
对象,则得到它们之间的毫秒数。然后,您可以简单地将该值除以一天中的毫秒数,得到日期之间的天数。

您做得很好,但是您需要利用JavaScript中内置的日期计算,使用
date

具体而言,要获取从日期到日期之间的天数,可以使用以下逻辑:

var currentDate = new Date();    // This will get today's date
currentDate.setHours(0,0,0,0);   // Remove time from consideration
                                 // As recommended by @NickSlash

// The following get's the lease expiration date using the year,
// month and date that you have already extracted from the input
var leaseExpirationDate = new Date( lyyyy, lmm, ldd ); 

// The number of days remaining in the lease is simply the following:
var one_day = 1000*60*60*24;
var daysLeftInLease = (leaseExpirationDate - currentDate ) / one_day;

之所以会出现这种神奇现象,是因为在内部,
Date
类将日期值保留为自1970年1月1日以来的毫秒数。因此,如果减去两个
Date
对象,则得到它们之间的毫秒数。然后,您可以将该值除以一天中的毫秒数,得到日期之间的天数。

也许可以帮助您。我在这方面遇到了XY问题。我看到了你的代码,我或多或少看到了你想要实现的目标,但因为你没有真正提出问题,而且因为我只看到最终的代码,而没有看到原始的“规格计算”,所以我无法很容易地确定你是否创建了好的代码。moment.js看起来可以很方便地减少一些大小,谢谢Niels-提供的唯一数据