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

Discussion in 'Javascript' started by Tom, Jun 17, 2004.

  1. Tom

    Tom Guest

    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,
    Tom, Jun 17, 2004
    #1
    1. Advertising

  2. 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.
    Richard Cornford, Jun 17, 2004
    #2
    1. Advertising

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

    JRS: In article <>, seen
    in news:comp.lang.javascript, Tom <> 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.
    Dr John Stockton, Jun 17, 2004
    #3
  4. Tom

    Shawn Milo Guest

    (Tom) wrote in message news:<>...
    > 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,



    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.');
    Shawn Milo, Jun 17, 2004
    #4
  5. Tom

    Randy Webb Guest

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

    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/
    Randy Webb, Jun 17, 2004
    #5
  6. Rookie Question: Need a Script To Count How Many Days Have Passed Since January 1, 1970

    JRS: In article <>, seen
    in news:comp.lang.javascript, Shawn Milo <>
    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.
    Dr John Stockton, Jun 17, 2004
    #6
  7. Tom

    Tom Guest

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

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





    Dr John Stockton <> wrote in message news:<>...
    > JRS: In article <>, seen
    > in news:comp.lang.javascript, Tom <> 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 ...
    Tom, Jun 18, 2004
    #7
  8. Re: Rookie Question: Need a Script To Count How Many Days Have Passed Since January 1, 1970

    JRS: In article <>, seen
    in news:comp.lang.javascript, Tom <> 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.
    Dr John Stockton, Jun 18, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vertigo

    days since 1 Jan 1970

    vertigo, Jul 7, 2004, in forum: Perl
    Replies:
    6
    Views:
    7,311
    Joe Smith
    Jul 8, 2004
  2. cyril
    Replies:
    4
    Views:
    1,645
    cyril
    Aug 25, 2003
  3. Summu82
    Replies:
    5
    Views:
    862
    Richard Heathfield
    Jun 7, 2006
  4. JC
    Replies:
    4
    Views:
    3,241
    Marcel Müller
    Jan 7, 2009
  5. Replies:
    3
    Views:
    176
    Dr J R Stockton
    May 21, 2007
Loading...

Share This Page