我需要将任意毫秒数转换为天、小时、分钟和秒。
例如:10天,5小时,13分钟,1秒。
我需要将任意毫秒数转换为天、小时、分钟和秒。
例如:10天,5小时,13分钟,1秒。
好的,既然没有其他人站出来,我就编写简单的代码来完成这个任务:
x = ms / 1000
seconds = x % 60
x /= 60
minutes = x % 60
x /= 60
hours = x % 24
x /= 24
days = x
我很高兴你只问了天数而没有问月数。:)
注意,在上面的代码中,假设/
表示截断整数除法。如果您在一种使用/
表示浮点除法的语言中使用此代码,则需要根据需要手动截断除法的结果。
设A为毫秒数,则有:
seconds=(A/1000)%60
minutes=(A/(1000*60))%60
hours=(A/(1000*60*60))%24
等等,(%
是求模运算符)。
希望这有所帮助。
以下两种解决方案都使用了JavaScript(我之前不知道这个解决方案是与编程语言无关的!)。如果需要捕获时间跨度> 1个月
,则两种解决方案都需要扩展。
var date = new Date(536643021);
var str = '';
str += date.getUTCDate()-1 + " days, ";
str += date.getUTCHours() + " hours, ";
str += date.getUTCMinutes() + " minutes, ";
str += date.getUTCSeconds() + " seconds, ";
str += date.getUTCMilliseconds() + " millis";
console.log(str);
结果为:
"6 days, 5 hours, 4 minutes, 3 seconds, 21 millis"
虽然库非常有用,但是为什么要用库呢?不如重新发明轮子! :)
var getDuration = function(millis){
var dur = {};
var units = [
{label:"millis", mod:1000},
{label:"seconds", mod:60},
{label:"minutes", mod:60},
{label:"hours", mod:24},
{label:"days", mod:31}
];
// calculate the individual unit values...
units.forEach(function(u){
millis = (millis - (dur[u.label] = (millis % u.mod))) / u.mod;
});
// convert object to a string representation...
var nonZero = function(u){ return dur[u.label]; };
dur.toString = function(){
return units
.reverse()
.filter(nonZero)
.map(function(u){
return dur[u.label] + " " + (dur[u.label]==1?u.label.slice(0,-1):u.label);
})
.join(', ');
};
return dur;
};
创建一个“duration”对象,其中包括您所需的任何字段。 格式化时间戳变得简单起来...console.log(getDuration(536643021).toString());
输出结果:
"6 days, 5 hours, 4 minutes, 3 seconds, 21 millis"
return dur[u.label] + " " + (dur[u.label]==1?u.label.slice(0,-1):u.label);
- Phillip KamikazeApache Commons Lang 提供了一个 DurationFormatUtils 工具类,其中包含非常实用的方法,例如formatDurationWords。
int milliseconds = someNumber;
int seconds = milliseconds / 1000;
int minutes = seconds / 60;
seconds %= 60;
int hours = minutes / 60;
minutes %= 60;
int days = hours / 24;
hours %= 24;
public static String formatMs(long millis) {
long hours = TimeUnit.MILLISECONDS.toHours(millis);
long mins = TimeUnit.MILLISECONDS.toMinutes(millis);
long secs = TimeUnit.MILLISECONDS.toSeconds(millis);
return String.format("%dh %d min, %d sec",
hours,
mins - TimeUnit.HOURS.toMinutes(hours),
secs - TimeUnit.MINUTES.toSeconds(mins)
);
}
12h 1 min, 34 sec
function convertTime(time) {
var millis= time % 1000;
time = parseInt(time/1000);
var seconds = time % 60;
time = parseInt(time/60);
var minutes = time % 60;
time = parseInt(time/60);
var hours = time % 24;
var out = "";
if(hours && hours > 0) out += hours + " " + ((hours == 1)?"hr":"hrs") + " ";
if(minutes && minutes > 0) out += minutes + " " + ((minutes == 1)?"min":"mins") + " ";
if(seconds && seconds > 0) out += seconds + " " + ((seconds == 1)?"sec":"secs") + " ";
if(millis&& millis> 0) out += millis+ " " + ((millis== 1)?"msec":"msecs") + " ";
return out.trim();
}
这是我编写的一个方法。它接受一个整数毫秒值
并返回一个可读性好的字符串
:
public String convertMS(int ms) {
int seconds = (int) ((ms / 1000) % 60);
int minutes = (int) (((ms / 1000) / 60) % 60);
int hours = (int) ((((ms / 1000) / 60) / 60) % 24);
String sec, min, hrs;
if(seconds<10) sec="0"+seconds;
else sec= ""+seconds;
if(minutes<10) min="0"+minutes;
else min= ""+minutes;
if(hours<10) hrs="0"+hours;
else hrs= ""+hours;
if(hours == 0) return min+":"+sec;
else return hrs+":"+min+":"+sec;
}
Long serverUptimeSeconds =
(System.currentTimeMillis() - SINCE_TIME_IN_MILLISECONDS) / 1000;
String serverUptimeText =
String.format("%d days %d hours %d minutes %d seconds",
serverUptimeSeconds / 86400,
( serverUptimeSeconds % 86400) / 3600 ,
((serverUptimeSeconds % 86400) % 3600 ) / 60,
((serverUptimeSeconds % 86400) % 3600 ) % 60
);