TwelvestoneFlash

AS3 - Time Conversion


Sign in

  • Waiting for Godot ( 720 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 4.8 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.8 k posts )
    general front end design an...
  • Back End ( 9.6 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
Storm
 
2008-10-05

Working on yet another video player and this client DEFINITELY WANTS (big babies) the time to read min:sec versus decimal numbers.

root.pTime.text = ns.time.toFixed(0); root.tTime.text = duration.toFixed(0);

So, I'm using toFixed but I'm wondering if any of your code geniuses can tell me how to convert ns.time (progress time) and duration (video duration) and simple min:sec without separate text boxes for minutes, seconds, colons and slashes for the time readout aka 0:08 / 1:49 in the Flash file with 0:08 being time played and 1:49 being total duration with a slash in between. This is what the Creative Director wants.

I can do the Date() conversions but that seems like a pain in the ass because under 10 seconds I can't get 01, 02 instead of 1,2, etc if you know what I mean.

Any quick solutions? Adobe help sucks ass. I found nothing after an hour that is helpful for use devigners.

JLM
 
2008-10-06

You can use date tricks to output something with colon from millisecond input but not sure it is optimal processing wise or if that is what you want?

import flash.utils.Timer;
import flash.events.TimerEvent;
var d: Date = new Date();
var count: int = 0;
var t: Timer= new Timer(100, 0);

function traceTime( e: TimerEvent )
{


    count++;
var millisec:int = count*100;
trace( millisec )
trace( formatWithColons( millisec ) );

}

function formatWithColons( tim: int )
{
var t2: int  = d.getTime();
    var d2: Date = new Date( new Date(t2+ tim ).getTime() - t2);
var str: String =  d2.toString().split(' ')[3];

// you prob don't want the first 0?
///return str.substr(-4,4); 

return str.substr(-5,5);    
}

t.addEventListener( TimerEvent.TIMER, traceTime );
t.start();
Storm
 
2008-10-06

I'm not good at explaining my requests. Thanks JLM. That is exactly what I wanted, but like you said, I don't think it would be optimal doing that every frame. And honestly, I was hoping there might be something new in AS3 and I missed it where there was a convertToColonBasedTime() (or something) function already written by Adobe.

I'm just going to go with the milliseconds. I've put enough effort into this Video Player.

Thanks JLM. You are a code genius.

lithium
 
2008-10-06

package com.snepo.videoplayer.util {

/**
 * @author andrewwright
 */
public class TimeUtil 
{
            /** returns time in hh:mm:ss format from seconds **/
    public static function formatTime ( time:Number ):String
    {
        var remainder:Number;

        var hours:Number = time / ( 60 * 60 );

        remainder = hours - (Math.floor ( hours ));

        hours = Math.floor ( hours );

        var minutes = remainder * 60;

        remainder = minutes - (Math.floor ( minutes ));

        minutes = Math.floor ( minutes );

        var seconds = remainder * 60;

        remainder = seconds - (Math.floor ( seconds ));

        seconds = Math.floor ( seconds );

        var hString:String = hours < 10 ? "0" + hours : "" + hours; 
        var mString:String = minutes < 10 ? "0" + minutes : "" + minutes;
        var sString:String = seconds < 10 ? "0" + seconds : "" + seconds;

        if ( time < 0 || isNaN(time)) return "00:00";           

        if ( hours > 0 )
        {           
            return hString + ":" + mString + ":" + sString;
        }else
        {
            return mString + ":" + sString;
        }
    }
}

}

hope it helps,

lith

Storm
 
2008-10-07

fuck you guys are brilliant

thanks

lith, I used yours. It was easier to understand how to translate what I had already done into the conversion.

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

AS3 - Time Conversion