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

T

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, (e-mail address removed)
 
R

Richard Cornford

Tom said:
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.

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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
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 ...
 
S

Shawn Milo

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, (e-mail address removed)


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.');
 
R

Randy Webb

Shawn Milo wrote:
//milliseconds * seconds * minutes * hours
var oneDay = 1000 * 60 * 60 * 24;

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

Dr John Stockton

JRS: In article <[email protected]>, seen
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.
 
T

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","Aug","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 -->
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
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.


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top