Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Date arithmetic Date: Wed, 12 Nov 2014 13:24:01 +0000 Organization: A noiseless patient Spider Lines: 72 Message-ID: <87d28szh5a.fsf@bsb.me.uk> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="29417"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18c2/sbnDRuz19xYO3XoHeCyVfpVpLgh/s=" Cancel-Lock: sha1:P2R9tnPPzsf+Q9Sc9JLn2HAa1sM= sha1:YlUPV6Of7eBjDtqODChAfixZ+Ko= X-BSB-Auth: 1.dd12a63221118657c4cf.20141112132401GMT.87d28szh5a.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:26122 Swifty writes: > On 11/11/2014 14:10, Evertjan. wrote: >> Please consider doing this serverside, >> at least do not depend on the user client browser for your money. > > Ah, but recall, *I'm* the user client in this case. I can trust my own > browsers (mostly Chrome these days). What I can't trust is my puny > JavaScript skill (intentionally singular, to emphasize the lack). > > If the browser screws up our JavaScript efforts, then I'll fall back > on calculating the amount myself. But it always pains me to have such > a good computer, but still having to do the work myself. :-) > > The effect I'm looking for is that the ID=amount field will populate > automatically the instant there is data in the three fields "from", > "to", and "profile" contain values other than blank. You might add a listener to the change event for these three fields (you might even listen for key presses to get more immediate results). This listener function (the some one will do for all three) checks the fields for having usable values and fills in the fourth when that condition is met. For small bits of code like this, I like to use this pattern: window.addEventListener( 'load', function () { var from = document.getElementById('from'); var to = document.getElementById('to'); var cost = document.getElementById('cost'); var rate = document.getElementById('rate'); function checkAndCalc() { var rateVal = parseFloat(rate.value) if (!isNaN(rateVal) && from.valueAsDate !== null && to.valueAsDate !== null) { var weeks = Math.abs(to.valueAsDate - from.valueAsDate) / (7*24*60*60*1000); cost.value = (rateVal * weeks).toFixed(2); } }; from.addEventListener('change', checkAndCalc, false); to. addEventListener('change', checkAndCalc, false); rate.addEventListener('change', checkAndCalc, false); }, false ); Note: this is only a sketch (but I did test that it). It uses the new HTML5 API (valueAsDate) so the date input elements should have type=date. As for getting the rate/week from a profile name, that is better done on the server. I'd have the profiles in a file and have some server script (PHP?) generate a select item like this: Then you could replace the "rate" input element with the profile menu. -- Ben.