views:

9533

answers:

8

I need to convert an arbitrary amount of milliseconds into Days, Hours, Minutes Second.

For example:

10 Days, 5 hours, 13 minutes, 1 second.

I'm pretty sure that the trick is fairly simple, but I'm slammed today so I thought I'd reach out to SO's geniuses for a quick fix.

Thanks :)

EDIT: The language I'm using does not have this built in, otherwise I'd use it.

EDIT 2: Not homework, I've been out of school for many years.

A: 

I would suggest using whatever date/time functions/libraries your language/framework of choice provides. Also check out string formatting functions as they often provide easy ways to pass date/timestamps and output a human readable string format.

theraccoonbear
+5  A: 

Let A be the amount of milliseconds. Then you have:

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

and so on (% is the modulus operator).

Hope this helps.

friol
+14  A: 

Well, since nobody else has stepped up, I'll write the easy code to do this:

x = ms / 1000
seconds = x % 60
x /= 60
minutes = x % 60
x /= 60
hours = x % 24
x /= 24
days = x

I'm just glad you stopped at days and didn't ask for months. :)

Greg Hewgill
Just used that in a flash function. thanks! (upvoted for simplicity)
Makram Saleh
A: 

Your choices are simple:

  1. Write the code to do the conversion (ie, divide by milliSecondsPerDay to get days and use the modulus to divide by milliSecondsPerHour to get hours and use the modulus to divide by milliSecondsPerMinute and divide by 1000 for seconds. milliSecondsPerMinute = 60000, milliSecondsPerHour = 60 * milliSecondsPerMinute, milliSecondsPerDay = 24 * milliSecondsPerHour.
  2. Use an operating routine of some kind. UNIX and Windows both have structures that you can get from a Ticks or seconds type value.
plinth
+1  A: 

You should use the datetime functions of whatever language you're using, but, just for fun here's the code:

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;
AlbertEin
A: 

I gotta question if anyne can answer it. I was able to create a method that converts the

milliseconds into hours, minutes and seconds but the problem is that I cant figure out how

to get the method to calculate the milliseconds as one value. what i mean is I did 3

separate calculations for the hours minutes and seconds and the method treats each of them

as separate calculations so when i test it and i enter 60000 milliseconds, it returns a

value of 00:01:60 when it should be 00:01:00 because there are 60,000 milliseconds in one

minute. Any help would be appreciated.

James Eldin
Friendly advise: You better ask a question in an separate Question on SO, otherwise not much people will see / answer it. Nor will you receive reputation for it.
Webleeuw
A: 

Take a look at How to convert Milliseconds to “X mins, x seconds” in Java? it suggests to use the java.util.concurrent.TimeUnit class

lrkwz
A: 
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
);
Krolique