Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert seconds to days: hours:minutes:seconds

Tags:

datetime

r

I want to convert the seconds into hours minutes and seconds in R.

Example

86400seconds-1day  gmdate("d H:i:s",86400) 

This is how I tried.

like image 358
surendra Avatar asked Dec 05 '14 09:12

surendra


People also ask

How do you convert days seconds to hours minutes and seconds?

int seconds = (totalSeconds % 60); int minutes = (totalSeconds % 3600) / 60; int hours = (totalSeconds % 86400) / 3600; int days = (totalSeconds % (86400 * 30)) / 86400; First line - We get the remainder of seconds when dividing by number of seconds in a minutes.

How do you convert seconds to DD HH mm SS?

To do the conversion yourself follow these steps. Find the number of whole hours by dividing the number of seconds by 3,600. The number to the left of the decimal point is the number of whole hours. The number to the right of the decimal point is the number of partial hours.


1 Answers

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" 
like image 197
akrun Avatar answered Oct 01 '22 20:10

akrun