Javascript: calculating remaining time (and then rent) on a tenancy (Rent Calculator) -


i work @ corporate estate agency , i'm creating internal page staff use contain few small tools them use among other things.

one of things i've been trying put rent calculator, which, given rental amount , lease expiry date user, needs determine time left (from today) on lease , advise how rent left pay on lease.

i've got working, mostly, can see it's long (i'm assuming extent has be) , feel reasonably messy:

//calculates remaining rent left pay terminated leases $("input[name='calcrent']").click(function() {     //gets rent , daily rent 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 , today's date day, month, year parts     //so units can 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 expiry month next year (or later) add 12 expiry         //month value make "lmm - tmm" calculation give positive value         if (lyyyy > tyyyy) {             lmm += (12 * (lyyyy - tyyyy));         }     //takes current month expiry month number of     //whole months left in lease, checks day values see whether     //we have passed rent due date month (and there's     //one less whole month left thought), taking 1     //wholemths value if     var wholemths = lmm - tmm;         if (ldd == (tdd - 1)) {             wholemths = wholemths;         } else if (ldd < (tdd - 1)) {             wholemths -= 1;         }     //works out if there days charged @ daily rate (i.e. not     //part of whole month). if today's date(tdd) == expiry date(ldd)+1 have no     //leftover days (rental month runs so: 12/04 - 11/05). if tdd > ldd+1     //(leftover days cross on month end) set checkmonth true 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 above line: "daysleft = (31 - tdd) + ldd;" assume months have 31 days         //as majority do, if checks whether month end cross on our         //leftover days has 30 days, if not check whether it's february , whether         //it's leap year appropriate no. of days charge - if meets         //any of these criteria relevant no. of days subtracted 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));         }); 

one of bits that's bugging me lease expiry occurs in subsequent year. haven't got head round how tidily deal 'value' of month (as can see in final if).

any suggestions on appreciated number , volume of comments seem bit disproportionate actual code - think they're necessary @ moment it's not clear what's going on.

thanks,

good work on have done, need take advantage of built-in date calculations available in javascript using date class.

specifically number of days between dates can use following logic:

var currentdate = new date();    // today's date currentdate.sethours(0,0,0,0);   // remove time consideration                                  // recommended @nickslash  // following get's lease expiration date using year, // month , date have extracted input var leaseexpirationdate = new date( lyyyy, lmm, ldd );   // number of days remaining in lease following: var one_day = 1000*60*60*24; var daysleftinlease = (leaseexpirationdate - currentdate ) / one_day; 

this magic happens because internally date class keeps date value number milliseconds since 1/1/1970. therefore if subtract 2 date objects number of milliseconds between them. can divide value number of milliseconds in 1 day number of days between dates.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -