在Actionscript 3中将秒转换为分钟和秒

时间:2012-04-02 21:27:57

标签: actionscript-3 actionscript

我正在使用youtube API,我在视频上获得了当前时间。 我想要做的是将它们转换成:MM:SS

我试图谷歌并自己尝试不同的东西,但似乎没有任何工作和效率。

我很高兴看到如何以一种良好而有效的方式提前做到这一点!

4 个答案:

答案 0 :(得分:6)

类似的东西:

var formattedTime = 
    (Math.floor(seconds/60)) + ":" + // minutes 
    (seconds % 60 >= 10 ? "": "0") + // padding for seconds if needed  
    (seconds % 60));                 // seconds 

答案 1 :(得分:1)

这里有一个小班,我一直用它来满足你的需要。我还为秒方法添加了时间码。只需使用Timecodes.secondsToTimecode(634);即可输出00:10:34

package com.ronnieswietek.utils
{
    public class Timecodes
    {
        public function Timecodes()
        {
        }

        public static function timecodeToSeconds(tcStr:String):Number
        {
            var t:Array = tcStr.split(":");
            return (t[0] * 3600 + t[1] * 60 + t[2] * 1);
        }

        public static function secondsToTimecode(seconds:Number):String
        {
            var minutes:Number          = Math.floor(seconds/60);
            var remainingSec:Number     = seconds % 60;
            var remainingMinutes:Number = minutes % 60;
            var hours:Number            = Math.floor(minutes/60);
            var floatSeconds:Number     = Math.floor((remainingSec - Math.floor(remainingSec))*100);
            remainingSec                = Math.floor(remainingSec);

            return getTwoDigits(hours) + ":" + getTwoDigits(remainingMinutes) + ":" + getTwoDigits(remainingSec);
        }

        private static function getTwoDigits(number:Number):String
        {
            if (number < 10)
            {
                return "0" + number;
            }
            else
            {
                return number + "";
            }
        }
    }
}

答案 2 :(得分:1)

var timeStr:String;
//Video's length >= 1 hour
if( seconds >= 60*60 ){
    //Format-> H:MM:SS
    timeStr = (""+Math.floor(seconds/(60*60))) + //Hours
               ":"+
              ("0"+Math.floor((seconds%(60*60))/60)).substr(-2)+ //Minutes                   
               ":"+
              ("0"+(seconds%60)).substr(-2);         //Seconds
}else{
    //Format-> MM:SS
    timeStr = ("0"+Math.floor(seconds/60)).substr(-2)+ //Minutes
               ":"+
              ("0"+(seconds%60)).substr(-2);         //Seconds
}

答案 3 :(得分:0)

更容易理解的东西让你了解它:

var seconds:int = 200;
var minutes:int = 0;

while(seconds >= 60)
{
    seconds -= 60;
    minutes ++;
}

trace(minutes, seconds);

前导零部分可以像这样完成:

var secStr:String = String(seconds);
var minStr:String = String(minutes);

secStr = (secStr.length == 1) ? "0" + secStr : secStr;
minStr = (minStr.length == 1) ? "0" + minStr : minStr;

trace(minStr + ":" + secStr);