How to Convert Milliseconds to Minutes and Seconds

Flash ActionScript 2.0

Someone recently asked in the Adobe Flash ActionScript forum how to convert milliseconds into the minutes-and-seconds format mm:ss.  This person wanted to format the value returned by Sound.duration in order to populate a dynamic text field to indicate the progression of a song — certainly a reasonable endeavor.  I also think it’s a fun way to demonstrate how to “roll your own” in cases where ActionScript lacks native support for whatever you’re hoping to achieve.  I’ll recount here what I posted in reply. 

An answer, short and sweet

Start a new FLA and place a dynamic text field into its own layer.  Using the Property inspector, give this text field the instance name output.  Create a scripts layer and type the following ActionScript into frame 1 (this code assumes you have an MP3 file named audio.mp3 in the same folder as the published SWF).

var music:Sound = new Sound();
music.loadSound("audio.mp3", true);
var minutes:Number = 0;
var seconds:Number = 0;
this.onEnterFrame = function() {
  minutes = Math.floor(music.position / 1000 / 60);
  seconds = Math.floor(music.position / 1000) % 60;
  output.text = minutes + ":" + seconds;
}

How it works

An arbitrarily named variable, music, is declared and set to an instance of the Sound class.  The Sound.loadSound() method is invoked on this instance, which causes the external file audio.mp3 to load and begin to play as soon as enough audio data become available.  This happens because the second method parameter, responsible for streaming (actually progressive downloading) is set to true.  Another two arbitrarily named variables, minutes and seconds, are declared as numbers and initialized to zero.

Next, a function is assigned to the MovieClip.onEnterFrame event of the main timeline (the global property this refers to the main timeline because that’s where this script is written).  At a default speed of twelve times a second (configurable via the movie’s framerate), the following occurs:

  • The minutes variable is set to the value of Sound.position, as referenced against the music instance.  Sound.position returns a value in milliseconds of the current position of the audio (at one second in, Sound.position returns 1000).  Actually, minutes is set to the milliseconds value divided by 1000, which provides seconds, then divided again by 60, which provides minutes.  This value is run through Math.floor() to round it down to the nearest integer.
  • The seconds variable is set to the value of Sound.position divided by 1000 — again, to provide seconds — but in addition, it is evaluated against the modulo operator (%) so that it never goes higher than 59 (see Lesser Known Operators:  Modulo (%) for details).  This value is also rounded down to the nearest integer.
  • Finally, the TextField.text property of the output instance is set to a concatenation of minutes, a colon (:), and seconds.

Variation

Usually, when time is displayed, numbers less than ten are shown with a leading zero.  In ActionScript, a leading zero refers to an octal number, rather than decimal, so leading zeroes would have be strings in this scenario.  The concatenation above takes care that, so you could, for example, use two conditional operators (?:) to compactly provide leading zeroes (see Lesser Known Operators:  Conditional (?:) for details).

Update the above output.text line to look like this:

output.text = ((minutes < 10) ? "0" + minutes : minutes) + ":" +
((seconds < 10) ? "0" + seconds : seconds);

Thanks to Brad Maylor for bringing an error in the Variation section to my attention. I have corrected the code.

13 Responses to “How to Convert Milliseconds to Minutes and Seconds”

  1. Mat Says:

    Great little tutorial, thanks! The lesser known operators solved my headache :)

  2. Paul Says:

    Its the little things that make the difference. Thanks

  3. stef Says:

    thanks, saved me some much needed brain matter :p

  4. Esteban Says:

    Lesser known blogs; may this never be one of them.
    Thanks a lot!

  5. David Stiller Says:

    Thanks, Mat, Paul, stef, and Esteban!

  6. Discorax Says:

    To take this a step further…you can use this same logic for FLV’s as well.

    Just use myFLVPlayer.totalTime and myFLVPlayer.playheadTime. Both are already in seconds, so you have to convert them into milliseconds first, but after that…works the same.

  7. black widow Says:

    great tutorial! :)

  8. David Stiller Says:

    black widow,

    Thanks!

  9. Denise Myers Says:

    You seem to have the answers to everything! Thank you!

  10. David Stiller Says:

    Denise,

    I sure wish I did! ;) But I’m certainly glad this blog entry was helpful to you.

  11. Robert Abramski Says:

    I never could find any practical purpose for the modulo (%) so I never use it. This is a good example where this operator is helpful. Reading what it does in the manual doesn’t really give a great illustration of the power of it. This does. Thanks for the tutorial. Somebody should make a common conversion class with conversions like this in it.

  12. casty Says:

    Thanks. Exactly what i need :)

  13. David Stiller Says:

    To Robert …

    I hear ya! I also find practical examples to be helpful. Sometimes a number of these API tools don’t seem to have value, otherwise.

    Even more often than time conversions, I tend to use modulo for determining odd and even numbers, such as when I’m coloring every other row a particular color.

    To casty …

    Glad to hear it!

Leave a Reply