[js常用]将秒转化为时分秒
生活随笔
收集整理的這篇文章主要介紹了
[js常用]将秒转化为时分秒
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
內容引入至網絡
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>秒轉換為時分秒</title>
<script src="http://code.jquery.com/jquery-1.11.2.js"></script>
</head>
<body>
<input type="text" id="demo1">s
<button id="btn">轉換</button>
<input type="text" id="demo2">或
<input type="text" id="demo3">
<script language="javascript">
/**
* 將秒數換成時分秒格式
*/
function formatSeconds(value) {
var theTime = parseInt(value);// 秒
var theTime1 = 0;// 分
var theTime2 = 0;// 小時
if(theTime > 60) {
theTime1 = parseInt(theTime/60);
theTime = parseInt(theTime%60);
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;
}
function formatSeconds2(a) {
var hh = parseInt(a/3600);
if(hh<10) hh = "0" + hh;
var mm = parseInt((a-hh*3600)/60);
if(mm<10) mm = "0" + mm;
var ss = parseInt((a-hh*3600)%60);
if(ss<10) ss = "0" + ss;
var length = hh + ":" + mm + ":" + ss;
if(a>0){
return length;
}else{
return "NaN";
}
}
</script>
<script>
$("#btn").on( "click", function( event ) {
var x = $("#demo1").val();
var y = formatSeconds(x);
$("#demo2").val(y);
$("#demo3").val(formatSeconds2(x));
});
</script>
</body>
</html>
總結
以上是生活随笔為你收集整理的[js常用]将秒转化为时分秒的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单数学(持续更新)
- 下一篇: python 字典操作方法详解