Skip to content

Instantly share code, notes, and snippets.

@keelii
Created June 21, 2013 06:05
Show Gist options
  • Save keelii/5829209 to your computer and use it in GitHub Desktop.
Save keelii/5829209 to your computer and use it in GitHub Desktop.
把秒数转换成 「天:时:分:秒」
var formatSecounds = function(seconds, callback) {
var rDays, rHours, rMinutes, rSeconds;
if (!seconds || seconds < 0) {
return;
} else {
seconds = Math.round(seconds);
}
//剩余天
rDays = seconds/(24*3600) | 0;
seconds = seconds - rDays*24*3600;
//剩余小时
rHours = seconds/3600 | 0;
seconds = seconds - rHours*3600;
//剩余分
rMinutes = seconds/60 | 0;
//剩余秒
rSeconds = seconds - rMinutes*60;
if (typeof callback === 'function') {
callback(rDays, rHours, rMinutes, rSeconds);
}
};
formatSecounds(1001, function(y, h, m, s) {
console.log(y + '天,' + h + '时,' + m + '分,' + s + '秒。');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment