Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: "Christoph M. Becker" Newsgroups: comp.lang.javascript Subject: Re: Date arithmetic Date: Fri, 14 Nov 2014 14:16:45 +0100 Organization: solani.org Lines: 48 Message-ID: References: <111120142237479787%timstreater@greenbee.net> <6814026.8sujcrue1K@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: solani.org 1415970997 15806 eJwNyUkBwDAIBEBLnAuRUwL4l5DOd1zBuGFwmK9vrk7TafK+owFjsVNUIsvxF6y+FIP4aGbIAxicEHY= (14 Nov 2014 13:16:37 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Fri, 14 Nov 2014 13:16:37 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 X-User-ID: eJwNyEkBwDAIBEBL3AtyKAn+JTTPGdfgGFh4mK9v1CSOkIgkc9/R23zuWII+wQvUaSz0qQg/GB0RAw== In-Reply-To: <6814026.8sujcrue1K@PointedEars.de> Cancel-Lock: sha1:OWNcZ4758v5PmkJVIeQLWXoBRAQ= X-NNTP-Posting-Host: eJwNyMEBwDAIw8CVCjYmjNOSsP8IzU+ngEydVIgxMX2Jgj9cd+0Eb1QJTVup9+jbKarM53jhB+u2D3c= Xref: csiph.com comp.lang.javascript:26137 Thomas 'PointedEars' Lahn wrote: > Christoph M. Becker wrote: > >> function daysBetween(isoDay1, isoDay2) { >> var msPerDay = 24 * 60 * 60 * 1000, > > You can skip that inefficient computation on each call (unless optimized) by > using the easy-to-remember value 864e5 (there are 86'400 seconds in a > standard day). ACK >> parts = isoExp.exec(isoDay1); > > I find > > parts = isoDay1.match(isoExp); > > easier to read. I use RegExp.prototype.exec() only in loops, such as with > parsers, because with global matching there is no other way to get at the > submatches. ACK. Frankly, I had copied the regexp handling from the FAQ[1]. It might be appropriate to change the code there, or at least adding a respective comment. Hopefully, that is possible without a formal vote. :) > However, you may want to skip the Math.abs() to give an indication to the > caller which date is in the future of the other. Rounding should also be > considered because those values are internally represented as floating-point > values. Omitting Math.abs() may or may not be desirable, depending on the usage. In pratice I would round to be on the safe side, but for demonstration purposes I deliberately omitted the rounding to clarify that this calculation is supposed to be exact, opposed to the Evertjan's suggestion. > JFTR, JSX:date.js (see sig) contains jsx.date.dayOfYear() and optionally > provides Date.prototype.getDayOfYear() which can also be used to compute the > difference. The calculation in jsx.date.dateOfYear() is rather intriguing; I wasn't aware of that formula. Thanks. [1] -- Christoph M. Becker