Convert Seconds to Days: Hours:Minutes:Seconds

Convert seconds to days, minutes, and hours in Obj-c

In this case, you simply need to divide.

days = num_seconds / (60 * 60 * 24);
num_seconds -= days * (60 * 60 * 24);
hours = num_seconds / (60 * 60);
num_seconds -= hours * (60 * 60);
minutes = num_seconds / 60;

For more sophisticated date calculations, such as the number of days within the ten million seconds after 3pm on January 19th in 1983, you would use the NSCalendar class along with NSDateComponents. Apple's date and time programming guide helps you here.

Converting seconds into days, hours, minutes & seconds in Python

It looks like you're trying to do something like:

result = "time in minutes is"
if days >0:
result += f" {days} days"
if hours > 0:
result += f" {hours} hours"
if mins > 0:
result += f" {mins} minutes"
if secs > 0:
result += f" {secs} seconds"

Convert seconds to days: hours:minutes:seconds

You may try

library(lubridate)
seconds_to_period(86400)
#[1] "1d 0H 0M 0S"

seconds_to_period(48000)
#[1] "13H 20M 0S"

If you need to format

td <- seconds_to_period(86400)
sprintf('%02d %02d:%02d:%02d', day(td), td@hour, minute(td), second(td))
#[1] "01 00:00:00"

If it spans for >99 days,

td <- seconds_to_period(1e7)
sprintf('%03d %02d:%02d:%02d', day(td), td@hour, minute(td), second(td))
#[1] "115 17:46:40"

Converting Seconds into days, hours, minutes, and seconds

Converting every variable to integer should erase decimals as you want, so use it like d = int(x/86400) for all of your variables (d, h, m and s).

Java: convert seconds into day, hour, minute and seconds using TimeUnit

It should be like

 int day = (int)TimeUnit.SECONDS.toDays(seconds);        
long hours = TimeUnit.SECONDS.toHours(seconds) - (day *24);
long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);
long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60);

EDIT
Explanation:

  1. Day calculation is correct, it does not require explanation.
  2. TimeUnit.SECONDS.toHours(seconds) will give you direct conversion from seconds to hours without consideration for days you have already calculated. Minus the hours for days you already got i.e, day*24. You now got remaining hours.
  3. Same for minute and second. You need to minus the already got hour and minutes respectively.

Convert seconds to Days, Minutes and Seconds

One of the things about programming is that there is never just one way to do something. In fact if I were to set my mind to it, I might be able to come up with a dozen completely different ways to accomplish this. You're not missing anything if your code meets requirements.

For your amusement, here's a way to format up hours:minutes:seconds under Windows (elapsed is a double & represents number of seconds elapsed since... something)

sprintf_s<bufSize>(buf, "%01.0f:%02.0f:%02.2f", floor(elapsed/3600.0), floor(fmod(elapsed,3600.0)/60.0), fmod(elapsed,60.0));


Related Topics



Leave a reply



Submit