Javascript:计算租约上的剩余时间(然后租用)(Rent Calculator)

问题描述:

我在一家企业房地产机构工作,我正在创建一个内部页面供员工使用,其中将包含一些小工具供他们使用除其他事项外。Javascript:计算租约上的剩余时间(然后租用)(Rent Calculator)

我一直试图放在一起的一件事是一个租金计算器,租赁计算器,从用户的租赁金额和租赁到期日期,需要确定剩余(从今天)的租赁时间和然后通知租赁剩余的租金是多少。

我找到了工作,主要是,但你可以看到它的相当长的(我假设在一定程度上它是),我感觉相当凌乱:

//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));   
}); 

之一特别让我感到困扰的是在下一年出现租约到期的地方。我还没有弄清楚如何整理那个月的'价值'(正如你可以在最后看到的那样)。

对此的任何建议将不胜感激,因为评论的数量和数量似乎与实际代码有些不成比例 - 但我认为目前它们是必要的,因为它不太清楚发生了什么。

感谢,

+0

也许[moment.js](http://momentjs.com/)可以帮助你。 – JAM 2013-05-12 21:55:41

+3

我在这个问题上遇到了XY问题。我看到你的代码,我或多或少地看到你想要实现的目标,但是因为你没有真正提出问题,而且因为我只看到最终的代码而不是原来的“规范计算”,所以我不能真正轻松地确定您是否创建了好的代码。 – 2013-05-12 22:02:20

+0

moment.js看起来可能会在减小尺寸方面很方便,谢谢。 Niels - 在代码之外提供的唯一数据是月租金价格,以及租期到期的日期,产出仅仅是一个数字(从今天到上述支付的剩余租金数量日期)。我的数据没有问题,并且它正在输出余额,但是如上所述,我主要关心的是如果有点混乱,那么租约到期日在我们目前所在的年份之后的最后一年在哪里,以及是否有更好的方法来确定这一切? – Josh 2013-05-12 22:13:17

对你做了什么,但你需要好好把握好工作的内置可在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类保持日期值1/1/1970以来的毫秒数。因此,如果你减去两个Date对象,你会得到它们之间的毫秒数。然后,您可以简单地将该值除以一天中的毫秒数,以获取日期之间的天数。

+0

请记住,原始代码中的许多复杂计算实际上是由于不希望**执行基于unix时间戳的计算。租金通常是按月支付的,因此很多计算需要知道1月15日和3月15日是“相隔两个月”,而不是“取决于闰年的59或60天”。 – 2013-05-12 22:15:43

+1

函数(在问题中)返回(或设置)一个结果,这是剩余租金,它的计算方式如何不相关,我正要提出相同的答案,打我:D可能想添加'currentDate.setHours (0,0,0,0);'或将数字四舍五入,以便当前时间不会影响结果。 – NickSlash 2013-05-12 22:22:54

+0

我认为尼尔斯是正确的 - 因为租金是按月收费的(哪!=每日房价,否则每月租金在所有12个月内不会相同 - 仅在不是整月的时候才会变为每日房价),按照这种计算方式计算出不正确的数字,例如:尝试它在:租金= 100;租赁到期日期= 11/03/2014;我得到了1094.89这里应该是一个1000回合。 令人讨厌,因为它削减了很多的代码:) – Josh 2013-05-12 22:26:28