By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,715 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,715 IT Pros & Developers. It's quick & easy.

Rookie Question: Need a Script To Count How Many Days Have Passed Since January 1, 1970

P: n/a
Tom
Please help.

I need a quick little scrpit to place on a web page that will count
how many days have passed since January 1, 1970. I have ZERO
experience writing ANY scripts. Anyone have any suggestions?

As for what is displayed on the screen, I just want it to say:

Today is: 12578 (or however many days have passed since that date.)

Any ideas on how to do that? I haven't been able to find any idea how
to do this on the web.

Remember, I'm a super-rookie at this stuff!

Many thanks.

Tom, to******@yahoo.com
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Tom wrote:
I need a quick little scrpit to place on a web page that will
count how many days have passed since January 1, 1970. I have
ZERO experience writing ANY scripts. Anyone have any
suggestions?
That date (Jan 1 1970) has such obvious coincidence with a significant
factor in the way computers handle dates that I suspect that this
question is motivated by the impending end of an academic year.

<snip> Any ideas on how to do that? I haven't been able to find any
idea how to do this on the web.

<snip>

Locate the comp.lang.javascript newsgroup FAQ, search it for references
to dates/time and follow the associated links.

Richard.
Jul 23 '05 #2

P: n/a
In article <5b**************************@posting.google.com >,
to******@yahoo.com (Tom) wrote:
Please help.

I need a quick little scrpit to place on a web page that will count
how many days have passed since January 1, 1970. I have ZERO
experience writing ANY scripts. Anyone have any suggestions?

As for what is displayed on the screen, I just want it to say:

Today is: 12578 (or however many days have passed since that date.)

Any ideas on how to do that? I haven't been able to find any idea how
to do this on the web.

Remember, I'm a super-rookie at this stuff!

Many thanks.

Tom, to******@yahoo.com


I would recommend a book. Consider going down to your local library. I
have seen Javascript books in medium size cities. At least where I
live, a local library can get a book loaned from a larger library.

This link contains reference to a book and links to online references.
http://www.jibbering.com/faq/#FAQ3

Most of the links seem addressed to the serious JavaScript programmer.

The book mentioned in the above link is "javascript: The Definitive
Guide" and is probably the best book on JavaScript. For a programmer
with moderate amount of experience in other languages this is the book.
For a person new to programming, you will want to pick up the more
typical JavaScript book of around 300 pages before moving to this book.
Get one with lots of examples.

This link contains an example of calculating how old someone is given a
date of birth. It should contain similar logic to what you want.
http://developer.irt.org/script/29.htm

Here is a minimalist html file with javascript:
< <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>A very little JavaScript</title>
</head>
<body>
<p>Includes JavaScript.</p>
<script type="text/javascript">
//This is very basic JavaScript.
var theNumber = (1 + 2) * 3;
alert("theNumber = " + theNumber);
//Insert the number into the html document
document.write("<p>The answer is " + theNumber + ".<\/p>");
</script>
<p>I didn't say it included much.</P>
</body>
</html>
Robert
Jul 23 '05 #3

P: n/a
to******@yahoo.com (Tom) wrote in message news:<5b**************************@posting.google. com>...
Please help.

I need a quick little scrpit to place on a web page that will count
how many days have passed since January 1, 1970. I have ZERO
experience writing ANY scripts. Anyone have any suggestions?

As for what is displayed on the screen, I just want it to say:

Today is: 12578 (or however many days have passed since that date.)

Any ideas on how to do that? I haven't been able to find any idea how
to do this on the web.

Remember, I'm a super-rookie at this stuff!

Many thanks.

Tom, to******@yahoo.com

Try these. I hope you play around with them and learn something.
I've added comments, for your educational/reading pleasure.

Shawn
//milliseconds * seconds * minutes * hours
var oneDay = 1000 * 60 * 60 * 24;
var checkDate = new Date('1/1/1970 0:00');

//defaults to current date/time
var today = new Date();

//remove current time from date, make it midnight, as in
//the checkdate, above
today = new Date((today.getMonth() + 1) + '/' + today.getDate() + '/'
+ today.getFullYear() + ' 0:00');

//today minus checkdate, divided by number of milliseconds in a day
var numDays = (today - checkDate) / oneDay;

//result
alert(numDays + ' since January 1st, 1970.');

//or (another way to do it)

numDays = 0;
//count the days in a loop
while (today > checkDate){

//adding a number to a date results
//in a NaN (not a number). So we
//subtract then re-add one in order
//to turn the variable into a numeric
//type, then do the math
checkDate--;

//make up for the millisecond
//we took away
checkDate++;

checkDate = new Date(checkDate + oneDay);
numDays++;
}

//result
alert(numDays + ' since January 1st, 1970.');
Jul 23 '05 #4

P: n/a
Shawn Milo wrote:
<--snip-->


//milliseconds * seconds * minutes * hours
var oneDay = 1000 * 60 * 60 * 24;


Only on days that have that many seconds. Some have fewer, some have more.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #5

P: n/a
JRS: In article <5b**************************@posting.google.com >, seen
in news:comp.lang.javascript, Tom <to******@yahoo.com> posted at Wed, 16
Jun 2004 17:54:03 :

I need a quick little scrpit to place on a web page that will count
how many days have passed since January 1, 1970. I have ZERO
experience writing ANY scripts. Anyone have any suggestions?


The Web is international. Therefore you need to define whether you mean
1970-01-01 to be GMT, local to you, or local to the reader.

You also should consider the meaning of "passed".

At noon GMT on 1970-01-03 :
one day had passed since Jan 1 GMT; it was called Jan 2 GMT
two days had passed since Jan 1 0000h GMT; Jan 1 & Jan 2, GMT
it is currently the Third Day, counting from the origin.

IMHO, the easiest specification is that you want to show "the current
day number, based on 1970 Jan 1 GMT/local/user being Day 0 or Day 1".
There is no need to count anything.

There are two sensible ways of solving your problem, AFAICS : learn some
javascript, or purchase a script from a trustworthy source. For the
actual algorithm ...

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> JL / RC : FAQ for 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.
Jul 23 '05 #6

P: n/a
JRS: In article <c2**************************@posting.google.com >, seen
in news:comp.lang.javascript, Shawn Milo <ne********@linurati.net>
posted at Thu, 17 Jun 2004 06:51:11 :
Try these. I hope you play around with them and learn something.
I've added comments, for your educational/reading pleasure. //milliseconds * seconds * minutes * hours
var oneDay = 1000 * 60 * 60 * 24;
var checkDate = new Date('1/1/1970 0:00');

//defaults to current date/time
var today = new Date();

//remove current time from date, make it midnight, as in
//the checkdate, above
today = new Date((today.getMonth() + 1) + '/' + today.getDate() + '/'
+ today.getFullYear() + ' 0:00');
Code posted to News should not be allowed to be wrapped by the news
system; it should be composed to fit. See FAQ and/or FAQ Notes.
//today minus checkdate, divided by number of milliseconds in a day
var numDays = (today - checkDate) / oneDay;

//result
alert(numDays + ' since January 1st, 1970.');
Gives me 12585.958333333334 since January 1st, 1970. Maybe you live
in Arizona, Guam, Kenya, or suchlike.

//or (another way to do it)
but using parts of what precedes it.


numDays = 0;
//count the days in a loop
while (today > checkDate){

//adding a number to a date results
//in a NaN (not a number). So we
//subtract then re-add one in order
//to turn the variable into a numeric
//type, then do the math
checkDate--;

//make up for the millisecond
//we took away
checkDate++;

checkDate = new Date(checkDate + oneDay);
The operation new Date is not quick; one should not do over 12000 of
them where a couple will suffice. By reading the FAQ, you could have
discovered the use of unary + to convert anything to Number.
numDays++;
}

//result
alert(numDays + ' since January 1st, 1970.');


Gives me 12586 since January 1st, 1970 , after a perceptible delay.
Consider :

Answer =
Math.round( ( new Date().setHours(0) - new Date(1970,0,1) ) / 864e5 )

It assumes that the winter/summer clock change (which AFAIK never
exceeds an hour) is less than about half a day.

Note : that gives a zero-based count, 1970-01-01 = 0, now 12586; for a
1-based count, change the 1 to a 0.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #7

P: n/a
Tom
Thanks for all the input, folks!!

Someone sent the following script to me. It works beautifully! Again,
thanks to all for your input.

Tom

Here's the script:

<H2>
<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin

var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","A ug","Sep","Oct","Nov","Dec");
function countup(yr,m,d) {
var today=new Date();
var todayy=today.getYear();

// Y2K Fix by Isaac Powell
// http://onyx.idbsu.edu/~ipowell

if ((navigator.appName == "Microsoft Internet Explorer") && (todayy <
2000))
todayy="19" + todayy;
if (navigator.appName == "Netscape")
todayy=1900 + todayy;

var todaym=today.getMonth();
var todayd=today.getDate();
var todaystring=montharray[todaym]+" "+todayd+", "+todayy;
var paststring=montharray[m-1]+" "+d+", "+yr;
var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1);

document.write ( " Today is " + difference + " ");

}
countup(1970,01,01); // Date in format: (year,month,day)

// End -->
</script>

</H2>

<!-- Script Size: 0.99 KB -->

Dr John Stockton <sp**@merlyn.demon.co.uk> wrote in message news:<lz**************@merlyn.demon.co.uk>...
JRS: In article <5b**************************@posting.google.com >, seen
in news:comp.lang.javascript, Tom <to******@yahoo.com> posted at Wed, 16
Jun 2004 17:54:03 :

I need a quick little scrpit to place on a web page that will count
how many days have passed since January 1, 1970. I have ZERO
experience writing ANY scripts. Anyone have any suggestions?


The Web is international. Therefore you need to define whether you mean
1970-01-01 to be GMT, local to you, or local to the reader.

You also should consider the meaning of "passed".

At noon GMT on 1970-01-03 :
one day had passed since Jan 1 GMT; it was called Jan 2 GMT
two days had passed since Jan 1 0000h GMT; Jan 1 & Jan 2, GMT
it is currently the Third Day, counting from the origin.

IMHO, the easiest specification is that you want to show "the current
day number, based on 1970 Jan 1 GMT/local/user being Day 0 or Day 1".
There is no need to count anything.

There are two sensible ways of solving your problem, AFAICS : learn some
javascript, or purchase a script from a trustworthy source. For the
actual algorithm ...

Jul 23 '05 #8

P: n/a
JRS: In article <5b**************************@posting.google.com >, seen
in news:comp.lang.javascript, Tom <to******@yahoo.com> posted at Thu, 17
Jun 2004 19:13:19 :
Someone sent the following script to me. It works beautifully! Again,
thanks to all for your input.
Those who send answers by mail include those who fear, probably rightly,
that their material is not fit to be seen in public.

Here's the script: ... var today=new Date();
var todayy=today.getYear();

// Y2K Fix by Isaac Powell
// http://onyx.idbsu.edu/~ipowell

if ((navigator.appName == "Microsoft Internet Explorer") && (todayy <
2000))
todayy="19" + todayy;
if (navigator.appName == "Netscape")
todayy=1900 + todayy;
What happens for browsers that claim to be something other than one of
those?

You can be sure that Year 2000 has been reached (so a test above is
superfluous); you can reasonably assume that your work will be worthless
after an interval not exceeding 95.5 years. Therefore,
todayy = 2000 + today.getYear()%100 // fix before 2100
is safe, for all browsers.

<!-- Script Size: 0.99 KB -->
One line is enough.


Dr J


Responses should go after trimmed quotes - see FAQ. Non-compliance
vexes. Use of the FAQ should have led you to short solutions.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> JL / RC : FAQ for 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.
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.