Created
June 21, 2013 06:05
-
-
Save keelii/5829209 to your computer and use it in GitHub Desktop.
把秒数转换成 「天:时:分:秒」
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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