number to hours minutes and seconds code example
Example 1: how to convert seconds into days hours seconds js
function secondsToDhms(seconds) {
seconds = Number(seconds);
var d = Math.floor(seconds / (3600*24));
var h = Math.floor(seconds % (3600*24) / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 60);
var dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : "";
var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
return dDisplay + hDisplay + mDisplay + sDisplay;
}
Example 2: java milliseconds to days hours minutes seconds
package com.roytuts.time;
import java.util.concurrent.TimeUnit;
public class MillisToDayHrMinSec {
public static void main(String[] args) {
final long milliseconds = 5478965412358l;
final long dy = TimeUnit.MILLISECONDS.toDays(milliseconds);
final long hr = TimeUnit.MILLISECONDS.toHours(milliseconds)
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(milliseconds));
final long min = TimeUnit.MILLISECONDS.toMinutes(milliseconds)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliseconds));
final long sec = TimeUnit.MILLISECONDS.toSeconds(milliseconds)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds));
final long ms = TimeUnit.MILLISECONDS.toMillis(milliseconds)
- TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(milliseconds));
System.out
.println(String.format("%d Days %d Hours %d Minutes %d Seconds %d Milliseconds", dy, hr, min, sec, ms));
}
}