js秒數轉換時分秒方法


今天寫一個東西的時候 發現給出的是秒數。實在找不到直接的工具去轉換。

就去網上找了個轉換方法(有現成的就不寫了,以后再簡化下代碼)。 

 function formatSeconds(value) {
            var theTime = parseInt(value);//
            var theTime1 = 0;//
            var theTime2 = 0;// 小時
            // alert(theTime);
            if(theTime > 60) {
                theTime1 = parseInt(theTime/60);
                theTime = parseInt(theTime%60);
                // alert(theTime1+"-"+theTime);
                if(theTime1 > 60) {
                    theTime2 = parseInt(theTime1/60);
                    theTime1 = parseInt(theTime1%60);
                }
            }
            var result = ""+parseInt(theTime)+"秒";
            if(theTime1 > 0) {
                result = ""+parseInt(theTime1)+"分"+result;
            }
            if(theTime2 > 0) {
                result = ""+parseInt(theTime2)+"小時"+result;
            }
            return result;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM