A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: How do I count up from a certain date?

  1. #1
    Junior Member
    Join Date
    Oct 2003
    Posts
    22

    How do I count up from a certain date?

    I want to do a thing where a number is displayd, and it represents the number of days that has passed since I started the date.

    Or to put it in another way, I put the certain date in the actionscript, and it displays how many days had passed since that date, on the main stage, inside a text box.

    I'm using Flash MX.
    you paid 1500$ for a computer just to read signatures?

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    code:

    oldDate = new Date(2004,1,23); // february 23
    newDate = new Date(); // today
    elapsed = newDate - oldDate; // elapsed milliseconds

    // some sample information
    trace("old date: " + oldDate);
    trace("new date: " + newDate);
    trace(elapsed + " milliseconds elapsed");
    trace(Math.floor(elapsed/1000) + " seconds elapsed");
    trace(Math.floor(elapsed/(1000*60)) + " minutes elapsed");
    trace(Math.floor(elapsed/(1000*60*60)) + " hours elapsed");
    trace(Math.floor(elapsed/(1000*60*60*24)) + " days elapsed");
    trace(Math.floor(elapsed/(1000*60*60*24*7)) + " weeks elapsed");



    // to put it in a text box:
    daysElapsedTextBox.text = Math.floor(elapsed/(1000*60*60*24));

    Last edited by jbum; 05-31-2004 at 01:41 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2003
    Posts
    22
    I put in the instance name "daysElapsedTextBox" without the quotations, and changed the code to the following:

    oldDate = new Date(2004, 4, 31);
    // february 23
    newDate = new Date();
    // today
    elapsed = newDate-oldDate;
    // elapsed milliseconds
    // some sample information
    trace("old date: "+oldDate);
    trace("new date: "+newDate);
    trace(Math.floor(elapsed/(1000*60*60*24))+" days elapsed");
    // to put it in a text box:
    daysElapsedTextBox.text = Math.floor(elapsed/(1000*60*60*24));


    And I got the following error message when I opened the flash movie, in the debug window thingie:

    old date: Mon May 31 00:00:00 GMT+0200 2004
    new date: Mon May 31 20:43:29 GMT+0200 2004
    0 days elapsed

    It's called "Output" instead of "debug", but the window is just annoying.
    Last edited by MO(D)VDO; 05-31-2004 at 01:52 PM.
    you paid 1500$ for a computer just to read signatures?

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    The instance name of the textbox would be

    daysElapsedTextBox

    (or you can use something shorter, and then modify the code to match).

    I don't use variable names with textboxes. Instead I've written the code to modify the .text property. So you can leave the variable name blank.

  5. #5
    Junior Member
    Join Date
    Oct 2003
    Posts
    22
    I appreciate your help very, very much, but more help is needed.
    I edited the post above.

    Edit: oh okay, it doesn't do that in the movie itself.

    You rock.

    How do you cause the output window to shut the hell up?
    Last edited by MO(D)VDO; 05-31-2004 at 01:54 PM.
    you paid 1500$ for a computer just to read signatures?

  6. #6
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    The trace commands (which send text to the output window) may be removed to shut up the output window.

    They were intended to illustrate how to manipulate the numbers.

    Since you're not familiar with it, you should know that the trace() command is VERY useful for learning actionscript and debugging your scripts.

    * * *

    Are you still getting "0 days elapsed" though? Zero and not some other number? If so, change this line:

    elapsed = newDate-oldDate;


    to this:

    elapsed = newDate.getTime() - oldDate.getTime();
    Last edited by jbum; 05-31-2004 at 02:01 PM.

  7. #7
    Junior Member
    Join Date
    Oct 2003
    Posts
    22
    no no, all is good.
    you own.
    you paid 1500$ for a computer just to read signatures?

  8. #8
    Junior Member
    Join Date
    Jul 2008
    Posts
    1
    hello I'm very new and I'm trying to teach myself i have taken on a little job for my company because i thought i knew enough about flash i could be dangerous but i know nothing and now i realize that from looking around in here but if someone could help me i would very grateful

    this is my problem i learned how to make a nifty little count down timer from learnflash.com now i need to make that baby count up from a date the date represents the last injury we had on our division well no matter how hard i try i can not get it to work here is the code I'm trying to manipulate i have tried to replace the (-) with a (+) but i get numbers that are way off as far as the running seconds and minutes its mainly for dramatic effect but meaningful.

    OK nuf chatter here is the code



    this.onEnterFrame = function() {

    var todayate = new Date();
    var currentYear = today.getFullYear();
    var currentTime = today.getTime();

    var targetDateate = new Date(currentYear,8,01);
    var targetTime = targetDate.getTime();

    var timeLeft = targetTime - currentTime;

    var sec = Math.floor(timeLeft/1000);
    var min = Math.floor(sec/60);
    var hrs = Math.floor(min/60);
    var days = Math.floor(hrs/24);
    sec =string(sec % 60);
    if (sec.length < 2) {
    sec = "0" + sec;
    }
    min = string(min % 60);
    if (min.length < 10) {
    min = "0" + min;
    }
    hrs = string(hrs % 24);
    if (hrs.length < 2) {
    hrs = "0" + hrs;
    }
    days = string(days);

    var counter:String = days + ":" + hrs + ":" + min + ":" + sec;
    time_txt.text = counter;


    }

    not sure how to put it in that nifty little box you all do

    sorry bout the faces :

  9. #9
    Junior Member
    Join Date
    Apr 2009
    Posts
    1
    Thanks jbum for showing clearly how to show the total number of seconds since a date:
    trace(Math.floor(elapsed/1000) + " seconds elapsed");

    I was wondering if anyone could help me make it countup live. All I want to be able to show is the continuous number of seconds passed since a certain date but it's a bit beyond me!

    Many thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center