你好,我写了一个 秒 => 时分秒 的函数,是前台Javascript的代码,请参考:
function sToHms(s){s = Math.floor(s) //如果输入的是浮点数,则舍弃小数位
var h = Math.floor(s/3600) //计算得出小时数
h = '0'+h
}
var m = Math.floor(s/60-h*60) //计算得出分钟数
if(m<10){ //调整为两位数的格式
m = '0'+m
}
var s = s%60 //计算得出剩下的秒数
if(s<10){ //调整为两位数的格式
s = '0'+s
}
return h+':'+m+':'+s //最后连接成字符串并返回
}
调用方法
sToHms(3600) //将会返回 01:00:00如果你是需要PHP直接在后台就转化,也可以按照这个函数的思路改写成PHP代码。
<script language=JavaScript>var timerID = null
var timerRunning = false
function stopclock (){
if(timerRunning)
clearTimeout(timerID)
timerRunning = false}
function startclock () {
stopclock()
showtime()}
function showtime () {
var now = new Date()
var hours = now.getHours()
var minutes = now.getMinutes()
var seconds = now.getSeconds()
var timeValue = "" +((hours >= 12) ? "下午 " : "上午 " )
timeValue += ((hours >12) ? hours -12 :hours)
timeValue += ((minutes <10) ? ":0" : ":") + minutes
timeValue += ((seconds <10) ? ":0" : ":") + seconds
document.clock.thetime.value = timeValue
timerID = setTimeout("showtime()",1000)
timerRunning = true}
</SCRIPT>
<body onload=startclock()>
<form name=clock >
<input name=thetime style="font-size: 9ptcolor:#000000border:0" size=12>
</body>
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>demo</title> <script> window.onload = function(){ var monitor = document.getElementById('monitor') setInterval(function(){ var now = new Date() monitor.innerHTML = now.getHours()+':'+now.getMinutes()+':'+now.getSeconds() },1000) } </script></head><body> <div id="monitor"></div></body></html>欢迎分享,转载请注明来源:内存溢出
评论列表(0条)