JRS: In article <df*******************@twister.nyroc.rr.com>, dated
Tue, 6 Sep 2005 00:18:17, seen in news:comp.lang.javascript, Mick White
<mw***********@rochester.rr.com> posted :
edwardfredriks wrote: I'm looking for a script that, instead of counting down, can "count up"
from a given date. So the output should be something like "(xx) days
since (date/event)" or "(date/event) was (xx) days ago".
Does anybody know where to find a script like that, or could someone
code one for me?
Something like:
<script type="text/javascript">
function daysSince(dateObj){
return Math.floor((new Date()-dateObj)/864e5))
}
x=daysSince(new Date(2005,8,1))// Sep 1, 2005
...
Not entirely accurate, because of summer time and dependence on user's
clock.
If you use new Date(2005, 8, 1, 12) // noon 2005 Sep 01
and Math.round instead of Math.floor, all will be well except sometimes
for an hour at the start/end of a day; and that should be fixable by
appending .setHours(12) to new Date() .
For two dates given as Y1 M1 D1 Y2 M2 D2, however, it would be better to
use Date.UTC().
I wonder whether the OP thought to read the newsgroup FAQ, which should
have lead him to function DateDiff .
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.