473,748 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4671
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.javas cript 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("theNumbe r = " + 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.go ogle.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.get Month() + 1) + '/' + today.getDate() + '/'
+ today.getFullYe ar() + ' 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.javas cript 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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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********@lin urati.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.get Month() + 1) + '/' + today.getDate() + '/'
+ today.getFullYe ar() + ' 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(numDay s + ' since January 1st, 1970.');
Gives me 12585.958333333 334 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(numDay s + ' 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.demo n.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demo n.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="JavaS cript">

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

<!-- Begin

var montharray=new Array("Jan","Fe b","Mar","Apr", "May","Jun","Ju l","Aug","Sep", "Oct","Nov","De c");
function countup(yr,m,d) {
var today=new Date();
var todayy=today.ge tYear();

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

if ((navigator.app Name == "Microsoft Internet Explorer") && (todayy <
2000))
todayy="19" + todayy;
if (navigator.appN ame == "Netscape")
todayy=1900 + todayy;

var todaym=today.ge tMonth();
var todayd=today.ge tDate();
var todaystring=mon tharray[todaym]+" "+todayd+", "+todayy;
var paststring=mont harray[m-1]+" "+d+", "+yr;
var difference=(Mat h.round((Date.p arse(todaystrin g)-Date.parse(past string))/(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.de mon.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.ge tYear();

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

if ((navigator.app Name == "Microsoft Internet Explorer") && (todayy <
2000))
todayy="19" + todayy;
if (navigator.appN ame == "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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
2435
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 Gregorian calendar. Since the Gregorian calendar did not begin until 15 Oct 1582 what is the purpose of dates before that date? Wouldn't any computation prior to that date be meaningless or am I missing something? The reason I ask is that I have...
16
2114
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 to build a date object. Then print the resulting date object, in it's full form. on the screen using document.write. This IS for school and I think I am starting out right, but can't quite seem to grasp what I am doing wrong, or where to go from...
9
2169
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? Regards, <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>
4
1564
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 === <script language=JavaScript> /* Date Count-up 1.0 (C) Copyright 1996 Ben Harold All rights Reserved
2
2445
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 experts. I've been asked to create a Daily Log sheet to be distributed to some of our clerks. For each day, the clerk is to log tasks worked on for the day, (i.e worked on the johnson account).
20
2975
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 "thatmonth" (10) it displays 0. With 9 it displays 2, instead of 1 which would be correct. Any suggestions? === Cut === <script language=Javascript type=text/javascript class="smalltext">
5
2515
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: System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj);
9
2419
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
3156
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 then the calender gone actually i want if i click outside off the calender then it should me removed ..How kan i do this ... Pls inform me as early as possible .. I am waiting for ur quick replay ...Here i attached the source code .... <!DOCTYPE...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.