使用场景: 需要将时间进行格式化的时候 ,不如后台传递到前台的是时间戳或者以秒为单位的时间数字 实现 需要使用到操作符 取余 %   除法 / 代码如下:

1
2
3
4
5
6
7
8
9
10

/**
* time 秒为单位
*/
function secondToTime(time){
var hours = Math.floor(time/3600);
var minute = Math.floor(time%3600/60);
var seconds = Math.ceil(time%3600%60);
return (hours == 0 ? '' : hours > 0 && hours.toString().length<2 ? '0'+hours+':' : hours+':') +(minute.toString().length<2 ? '0'+minute+':' : minute+":")+(seconds.toString().length<2 ? '0'+seconds : seconds);
}