Home >Web Front-end >JS Tutorial >How to convert time in seconds using javascript
Javascript method to convert seconds to time: first create a front-end code sample file; then use the p tag to store the converted time; and finally use the js code "var min=Math.floor(oTs.innerHTML600). .." can realize the time transfer in seconds.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
Convert seconds to time in native js
Regarding the application of operators, modulo should be more important. Today I will write an article about converting seconds to hours, minutes and seconds. Small example.
<span>11679</span>s是多少分钟 <p></p>
The p tag above is used to store the converted time. The js should be written like this:
window.onload=function(){ var oTs=document.getElementsByTagName("span")[0]; var time=document.getElementsByTagName("p")[0]; var min=Math.floor(oTs.innerHTML%3600); time.innerHTML = Math.floor(oTs.innerHTML/3600) + "时" + Math.floor(min/60) + "分"+ oTs.innerHTML%60 + "秒"; }
Rendering:
Of course There may not be many examples of this kind of direct conversion of seconds into hours, minutes and seconds, but its purpose is still to use modulo, and the main idea remains the same.
【Recommended learning: javascript advanced tutorial】
The above is the detailed content of How to convert time in seconds using javascript. For more information, please follow other related articles on the PHP Chinese website!