Need help converting text to time in duration format on Google Sheets
Asked Answered
K

3

2

Asking for some help here, im trying to convert text to time in duration format on Google Sheets, i´ve used some basic fuctions to to breakdown text (with delimiters as d (days) h (hour) m(minute) and s(second) into values that were then baked into a time function, however for outputs over 24 hours I was unable to get it to format properly i.e. in the image below 375 hrs should show 375:00:00 or [H]:mm:ss

Any ideas here?

Sharing the doc

https://docs.google.com/spreadsheets/d/1YWHM5tPaLOulHMbfdR8CZJsER7LBceWLQrm9f8JcV9c/edit#gid=0

enter image description here

Kicker answered 14/4, 2022 at 6:55 Comment(0)
B
0

try:

=FLATTEN(INDEX(QUERY(, "select  "&TEXTJOIN(",", 1, 
 SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B7:B27, 
 "d", "*86400"), "h", "*3600"), "m", "*60"), "s", "*1"), " ", "+")))/86400, 2))

enter image description here

Benuecongo answered 14/4, 2022 at 10:32 Comment(0)
W
1

Try, in J12

=(G12+H12/60+I12/60/60)/24

then apply duration format enter image description here

Woothen answered 14/4, 2022 at 7:15 Comment(0)
B
0

try:

=FLATTEN(INDEX(QUERY(, "select  "&TEXTJOIN(",", 1, 
 SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B7:B27, 
 "d", "*86400"), "h", "*3600"), "m", "*60"), "s", "*1"), " ", "+")))/86400, 2))

enter image description here

Benuecongo answered 14/4, 2022 at 10:32 Comment(0)
A
0

Use regexextract(), like this:

=arrayformula( 
  iferror(regexextract(B7:B, "(\d+)\s*h"), 0) & ":" & 
  iferror(regexextract(B7:B, "(\d+)\s*m"), 0) & ":" & 
  iferror(regexextract(B7:B, "(\d+)\s*s"), 0) 
)

The formula gives text strings. To get numeric durations, use value(), like this:

=arrayformula( 
  iferror( 1 / 
    value( 
      iferror(regexextract(B7:B, "(\d+)\s*h"), 0) & ":" & 
      iferror(regexextract(B7:B, "(\d+)\s*m"), 0) & ":" & 
      iferror(regexextract(B7:B, "(\d+)\s*s"), 0) 
    ) 
  ^ -1 ) 
)

Format the result column as Format > Number > Duration.

In the event there are many components in the text string you are converting, it may be beneficial to use just one regextextract(), which is possible using the recently introduced lambda functions:

=arrayformula( 
  map( 
    B7:B, 
    lambda( 
      duration, 
      if( 
        len(duration), 
        join( 
          ":", 
          iferror( 
            regexextract( 
              duration, 
              { "(\d+)\s*h"; "(\d+)\s*m"; "(\d+)\s*s" } 
            ), 
            0 
          ) 
        ), 
        iferror(1/0) 
      ) 
    ) 
  ) 
)

See this answer for an explanation of how date and time values work in spreadsheets.

Arcturus answered 13/11, 2022 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.