Skip to content

Instantly share code, notes, and snippets.

@suconghou
Created January 12, 2016 04:00
Show Gist options
  • Save suconghou/c854fdc15e06c0337023 to your computer and use it in GitHub Desktop.
Save suconghou/c854fdc15e06c0337023 to your computer and use it in GitHub Desktop.
Javascript辅助函数
// 格式化秒数到时间格式
Number.prototype.formatTime=function(){
// 计算
var h=0,i=0,s=parseInt(this);
if(s>60){
i=parseInt(s/60);
s=parseInt(s%60);
if(i > 60) {
h=parseInt(i/60);
i = parseInt(i%60);
}
}
// 补零
var zero=function(v){
return (v>>0)<10?"0"+v:v;
};
return [zero(h),zero(i),zero(s)].join(":");
};
alert(Number(90).formatTime()) //00:01:30秒
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment