473,385 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

counting up instead of counting down

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?

Thanks in advance.

Yours,
Edward Fredriks

Sep 6 '05 #1
6 1807
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
var days=
x>1?"days ago":x==1?"day ago":x== -1?"day from now":"days from now";

</script>

Not entirely accurate, because of summer time and dependence on user's
clock.(And x could equal zero)

Mick
Sep 6 '05 #2
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?


Depending on your requirements, Mick's solution may suffice. But if
more rigour is required, check out:

<URL:http://www.merlyn.demon.co.uk/js-date1.htm#diff>
--
Rob
Sep 6 '05 #3
Tried it (that is copy/pasted it into a html-file) but it didn't write
anything write anything in the browser window. Is there something else
I should do? I have virtually no understanding of the workings of
JavaScript whatsoever, so could you try to keep it plain. Thank you.

Edward

Mick White wrote:
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
var days=
x>1?"days ago":x==1?"day ago":x== -1?"day from now":"days from now";

</script>

Not entirely accurate, because of summer time and dependence on user's
clock.(And x could equal zero)

Mick


Sep 6 '05 #4
I have virtually no understanding of JavaScript whatsoever so this
looks like all gibberish to me, I have no clue what to do with it,
other than to plainly copy/paste it to a html-file (which didn't do
much good). So please, could you explain what to do with it to make it
work? Thank you.

Edward

Sep 6 '05 #5
edwardfredriks wrote:
Tried it (that is copy/pasted it into a html-file) but it didn't write
anything write anything in the browser window. Is there something else
I should do? I have virtually no understanding of the workings of
JavaScript whatsoever, so could you try to keep it plain. Thank you.


<script type="text/javascript">
function daysSince(dateObj){
return Math.floor((new Date()-dateObj)/864e5)
}
pastDate=new Date(2005,8,1)// Sep 1, 2005
x=daysSince(pastDate)
var days=
x>1?"days ago":x==1?"day ago":x== -1?"day from now":"days from now";
document.write(pastDate+" "+(x>0? "was":"is")+" "+x+" "+days)
</script>

Mick
Sep 6 '05 #6
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.
Sep 7 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does...
8
by: mgm | last post by:
hello, I have a query that is supposed to return only 1 record, however I recently found that because of an error in the database it can return more than 1. So what I need to do is capture if it...
9
by: David Poundall | last post by:
I have a thread class and I want to be able to track its usage within an application. FYI the class launches aplications in their own thread when the 'launch' method is called. That works OK ...
3
by: Megan | last post by:
hi everybody- i'm having a counting problem i hope you guys and gals could give me some help with. i have a query that retrieves a bevy of information from several different tables. first let...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
5
by: sololoquist | last post by:
#define COUNT_UP #include <stdio.h> #define N 10 int main() { int i; #ifdef COUNT_UP for (i = 0; i < N; i++)
27
by: Simon Biber | last post by:
I was reading http://en.wikipedia.org/wiki/Poker_probability which has a good description of how to count the frequency of different types of poker hands using a mathematical approach. A sample...
17
by: Razii | last post by:
This is specifically regarding U++ which is C++ libraries and IDE (please don't whine whether its on or off topic. Right click on the thread and click on ignore instead of wasting time). This is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.