[R] Minutes after midnight to time

Ben Tupper btupper at bigelow.org
Sat Dec 14 02:32:18 CET 2013


Hi,

On Dec 13, 2013, at 4:34 PM, Trevor Davies <davies.trevor at gmail.com> wrote:

> Is there a quick function that can convert minutes (seconds) after midnight
> to a time?
> 
> i.e 670.93 (minutes after midnight) --> 11:10:56.**
> 
> I know it can be done by hand but I thought there must be a function for
> this already.
> 

I'm not sure what you mean by doing it by hand, but do the following do the trick for you?

# convert seconds to hours
s2h <- function(x=670.93*60) {
   h <- floor(x/3600)
   m <- floor((x - h*3600)/60)
   s <- x - h*3600 - m*60
   sprintf("%0.2d:%0.2d:%6.3f", h, m, s)

}
# convert minutes to hours
m2h <- function(x=670.93) {
   h <- floor(x/60)
   m <- floor(x - h*60)
   s <- (x - h*60 - m) * 60
   sprintf("%0.2d:%0.2d:%6.3f", h, m, s)
}


> s2h() ; m2h()
[1] "11:10:55.800"
[1] "11:10:55.800"

Cheers,
Ben

> Thank you.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org



More information about the R-help mailing list