我在一家公司房地产中介公司工作,我正在创建一个供员工使用的内部页面,其中仅包含一些供他们使用的小工具。
我一直试图汇总的一件事是房租计算器,考虑到用户的租金金额和租约到期日期,它需要确定(从今天开始)剩余的时间,然后建议多少钱租金由该租约支付。
我大部分时间都可以使用它,但是如您所见,它已经相当长了(我在某种程度上认为是必须的),而且我感到相当混乱:
//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));
});
让我特别烦恼的一点是,下一年的租赁到期时间。我还没弄清楚如何整齐地处理该月的“价值”(如您在最后的if中所见)。
对此的任何建议都将受到赞赏,因为注释的数量和数量似乎与实际代码不成比例-但我认为目前尚有必要,因为目前尚不清楚情况如何。
谢谢,
最佳答案
所做的工作很好,但是您需要利用Date
类利用JavaScript中提供的内置日期计算功能。
具体来说,要获取日期之间的天数,可以使用以下逻辑:
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
对象,则将获得它们之间的毫秒数。然后,您可以将该值除以一天中的毫秒数,以获取日期之间的天数。