473,394 Members | 1,140 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,394 software developers and data experts.

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

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
8 4629
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dennis M. Marks | last post by:
After reading section 15.9.1.1 the ECMAScript Language Specifications I see that the date range for the Date function is +/- 100,000,000 days from 01 Jan 1970. This is called an extrapolated...
16
by: KL | last post by:
I am working on a problem and desperately need help! I need to prompt a user for the numerical month of birth, day of birth and year of birth and store it in varialbes and the use the variables...
9
by: beguigne | last post by:
Below is a snippet of a crude date picking routine for a form. I am a novice at this so, I am hitting my head at why when I select the day, the onChange event gives me a blank. What am I missing?...
4
by: none | last post by:
Below is an old count-up script that displays ok in MSIE with a bit of experimenting, but NS shows negative values that make no sense. Anyone know how to make it work ok in both? === Cut ===...
2
by: Test User | last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people as possible) I am somewhat familiar with Access 2000, but my latest project has me stumped. So, I defer to you...
20
by: none | last post by:
I have managed to get the below script *almost* working. However, it still has a problem calculating the number of months. The date I am trying to calculate from is Oct 15, 1994. With the correct...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
9
by: mistral | last post by:
Need help to remove list of days from date script. Need format "June 07, 2006" <SCRIPT LANGUAGE="JavaScript"> <!-- Begin // Get today's current date. var now = new Date();
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.