A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: comparing two dates

  1. #1
    judge and jury hawkmauk's Avatar
    Join Date
    Apr 2003
    Location
    buckinghamshire
    Posts
    122

    comparing two dates

    I have two buttons one to start my process and another to finish I would like to then work out the total time it took me to complete my process but don't know how to compare the two.
    Heres an aproximation of my code:

    code:

    btnStart.onPress = function(){
    started = new Date();
    }
    btnFinish.onPress = function (){
    finished = new Date();
    totalTime = new Date (finished - started);
    }



    if anyone could help me with displaying the time in a text box as 09:05 instead of 9:5 that would help me aswell -

    code:

    startText.text = started.getHours()+":"+ started.getMinutes();


    Mikee

  2. #2
    Senior Member
    Join Date
    Mar 2003
    Location
    127.0.0.1
    Posts
    154
    for your second problem, with displaying the time as 9:05, rather than 9:5, in the past i have just used an if statement for the seconds , if its less than 10, make a string = "0"+secs, and then display that string rahter than the actualy secs variable


    -hope that helps; =)
    -Steve

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    I normaly use getTimer() for the purpose you describe. However, sticking with your plan...

    Code:
    btnStart.onPress = function(){
            started = new Date();
    }
    btnFinish.onPress = function (){
            finished = new Date();
            totalTime = new Date (finished.getTime() - started.getTime());
    }
    
    // String Formatting is a ***** in actionscript, sadly.
    var hours = started.getHours();
    var mins = started.getMinutes();
    if (hours.length == 1)
      hours = '0' + hours;
    if (mins.length == 1)
      mins = '0' + mins;
    startText.text = hours + ':' + mins;
    The same code with getTimer()

    Code:
    btnStart.onPress = function(){
            started = getTimer();
    }
    btnFinish.onPress = function (){
            totalTime = getTimer() - started;
            // Or if you still want the date stuff...
            // totalTime = new Date(getTimer() - started);
    }
    
    // Who needs hours and minutes anyway? :)
    startText.text = (totalTime/1000) + " secs";

  4. #4
    judge and jury hawkmauk's Avatar
    Join Date
    Apr 2003
    Location
    buckinghamshire
    Posts
    122
    thanks people
    Mikee

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