A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: convert seconds to minutes

  1. #1
    Junior Member
    Join Date
    Apr 2002
    Posts
    20
    hi,

    i´m working on preloader and want to convert seconds from "time elapsed" and "time remaining" in minutes. Anyone got idea how can i make, let´s say 200 seconds to be 3´:33´´ ???.

    thanks

  2. #2
    Senior Member alex8735's Avatar
    Join Date
    May 2001
    Location
    Aachen, Germany
    Posts
    117
    I'm sure their is an easier way of doing it, but this should work:


    Code:
    num=200;
    minutes=Math.round(num/60);
    sec=num-(minutes*60);
    trace("time: "+minutes+":"+sec);
    //time: 3:20

  3. #3
    Junior Member
    Join Date
    Apr 2002
    Posts
    20
    thanks for quick reply I´ll try that now, if I came up with something else i´ll post it.

  4. #4
    Junior Member
    Join Date
    Apr 2002
    Posts
    20
    OK ! this works, i only used Math.floor instead Math.round because in preloading connection speed isn´t constant and i was getting some "-50" results for seconds. Thanks again...and if you are still with me...i have connection speed detection in preloader and when it is let´s say 2.00 kb/s it becomes just 2 in output, but i want it to be 2.00 so that numbers don´t jump left and right. Example, for seconds i used this:

    if (seconds1< 10){
    seconds = "0"+seconds1;
    }
    else{
    seconds = seconds1;
    }

    So the 9 sec becomes 09, and 24 stays 24...

    Any ideas ???

  5. #5
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    //format a number into specified number of decimal places
    Math.formatDecimals = function (num, digits) {
    //if no decimal places needed, we're done
    if (digits <= 0) {
    return Math.round(num);
    }
    //round the number to specified decimal places
    //e.g. 12.3456 to 3 digits (12.346) -> mult. by 1000, round, div. by 1000
    var tenToPower = Math.pow(10, digits);
    var cropped = String(Math.round(num * tenToPower) / tenToPower);

    //add decimal point if missing
    if (cropped.indexOf(".") == -1) {
    cropped += ".0"; //e.g. 5 -> 5.0 (at least one zero is needed)
    }

    //finally, force correct number of zeroes; add some if necessary
    var halves = cropped.split("."); //grab numbers to the right of the decimal
    //compare digits in right half of string to digits wanted
    var zerosNeeded = digits - halves[1].length; //number of zeros to add
    for (var i=1; i <= zerosNeeded; i++) {
    cropped += "0";
    }
    return(cropped);
    } //Robert Penner May 2001 - source@robertpenner.com

    my_num2 = Math.formatDecimals(1.1,2);
    trace (my_num2);


  6. #6
    Junior Member
    Join Date
    Apr 2002
    Posts
    20
    thanks i´ll try that

  7. #7
    Junior Member
    Join Date
    Apr 2002
    Posts
    20
    Ok, i´m lost and confused. I can´t get it right. Do i need all this code you give me? I tried using some parts but it doesn´t work.
    This is what i have:


    var loadedd = Math.floor(this.getBytesLoaded()/1024);
    var elapsed = Math.round(getTimer()/1000);

    //dividing this 2 var gives me connection speed
    var speed = ((Math.round((loadedd/elapsed)*100))/100);


    When var speed is 2 kb/s i want it to be 2.00 kb/s , or when it is 1.7 kb/s i want it to be 1.70 kb/s. What i´m doing wrong with all that code you post ??? I don´t think i should use all of it ....or am I ????

    thanks in advance

  8. #8
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    Yes, this is a function, and you should be using all of it...

    Put this in your first frame on the main timeline:

    //format a number into specified number of decimal places
    Math.formatDecimals = function (num, digits) {
    //if no decimal places needed, we're done
    if (digits <= 0) {
    return Math.round(num);
    }
    //round the number to specified decimal places
    //e.g. 12.3456 to 3 digits (12.346) -> mult. by 1000, round, div. by 1000
    var tenToPower = Math.pow(10, digits);
    var cropped = String(Math.round(num * tenToPower) / tenToPower);

    //add decimal point if missing
    if (cropped.indexOf(".") == -1) {
    cropped += ".0"; //e.g. 5 -> 5.0 (at least one zero is needed)
    }

    //finally, force correct number of zeroes; add some if necessary
    var halves = cropped.split("."); //grab numbers to the right of the decimal
    //compare digits in right half of string to digits wanted
    var zerosNeeded = digits - halves[1].length; //number of zeros to add
    for (var i=1; i <= zerosNeeded; i++) {
    cropped += "0";
    }
    return(cropped);
    } //Robert Penner May 2001 - source@robertpenner.com


    Then after your line var speed = ((Math.round((loadedd/elapsed)*100))/100);
    run your speed variable through it by adding the following:

    cor_speed = Math.formatDecimals(speed,2);

    Change the variable of the speed display textfield to cor_speed, although using this might still work:

    speed = Math.formatDecimals(speed,2);

  9. #9
    Junior Member
    Join Date
    Apr 2002
    Posts
    20
    it works !!! thank you man this is great!
    take care

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