Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert decimal day to HH:MM

I have a vector of decimal numbers, which represent 'decimal day', the fraction of a day. I want to convert it into HH:MM format using R.

For example, the number 0.8541667 would correspond to 20:30. How can I convert the numbers to HH:MM format?

like image 909
user5516342 Avatar asked Nov 03 '15 14:11

user5516342


2 Answers

Using chron:

chron::times(0.8541667)
#[1] 20:30:00
like image 93
nicola Avatar answered Sep 30 '22 20:09

nicola


Try this:

R> format(as.POSIXct(Sys.Date() + 0.8541667), "%H:%M", tz="UTC")
[1] "20:30"
R> 

We start with a date--which can be any date, so we use today--and add your desired fractional day.

We then convert the Date type into a Datetime object.

Finally, we format the hour and minute part of the Datetime object, ensuring that UTC is used for the timezone.

like image 28
Dirk Eddelbuettel Avatar answered Sep 30 '22 21:09

Dirk Eddelbuettel