date - 日期 - 如何将毫秒转换为人类可读形式?

我需要将任意的毫秒数转换为几天,小时,分钟,秒。

例如10天5小时13分钟1秒

时间:


x = ms / 1000


seconds = x % 60


x /= 60


minutes = x % 60


x /= 60


hours = x % 24


x /= 24


days = x



: )

设为毫秒数,


seconds=(A/1000)%60


minutes=(A/(1000*60))%60


hours=(A/(1000*60*60))%24



(%是模数运算符)。

希望这个能帮到你。

这两个解决方案都需要扩展,如果时间

解决方案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"



: )

解决方案2编写自己的解析器


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;


};



设置时间戳的格式就变得简单。


console.log(getDuration(536643021).toString());



给出:


"6 days, 5 hours, 4 minutes, 3 seconds, 21 millis"



Apache 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;



这是我写的一种方法,它接受一个integer milliseconds value,并且返回一个human-readable String


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;



}




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();


}



你的选择很简单:

  • 编写进行转换的代码,即除以毫秒数得到天,用模数除以毫秒数得到小时,用模数除以毫秒数得到秒,再除以1000得到秒,milliSecondsPerMinute = 60000 ,milliSecondsPerHour = 60 * milliSecondsPerMinute ,milliSecondsPerDay = 24 * milliSecondsPerHour .

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


);



...