poissonconsulting / dttr2

An R package to manipulate dates, times and date times
https://poissonconsulting.github.io/dttr2/
Other
10 stars 2 forks source link

Function for converting excel decimal times back to time format #35

Closed aylapear closed 18 hours ago

aylapear commented 8 months ago

This would undo the TIMEVALUE function in excel.

For example when your excel times read in as numeric values like 0.28125 which is actually 6:45

Had to do this for real data and this function worked for the small subset of values that had to be converted. None of the dttr2 excel convert functions worked for just doing the time.

excel_to_time <- function(x) {
  chk::chk_numeric(x)

  excel_time <- x * 24

  hour <- floor(excel_time)
  min <- excel_time %% 1 
  min <- min * 60
  min <- round(min)

  dtt_time_from_ints(hour, min, 0)
}
aylapear commented 8 months ago
> excel_to_time(0.28125)
06:45:00