Home  >  Article  >  Web Front-end  >  How to convert time in seconds using javascript

How to convert time in seconds using javascript

藏色散人
藏色散人Original
2021-04-23 09:58:313425browse

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.

How to convert time in seconds using javascript

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:

How to convert time in seconds using javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn