简体   繁体   中英

How do I convert a time string (2 hours 30 minutes) into a decimal so that it becomes 2.5?

In Excel or SQL, how can I convert the time string such as 2 hours 30 minutes into a decimal so that it reads 2.5 hours? This is in an excel spreadsheet so there are multiple values which also includes: 2 hours, 3 hours, 1 hour 15 minutes. Is there anyway to change all of the values using a formula or SQL query?

Keep it simple. Just do following Equation in Excel:

Minutes / 60 = Hours in Decimal

Example: 2 hours 30min

30 / 60 = 0.5  -> 2.5 hours

Example 2: 1 hour 15min

15 / 60 = 0.25  -> 1.25 hours

If you only use minutes: Example 3: 150min

150 / 60 = 2.5 hours

Its as easy as that :) Hope this helps.

From 2 hours 30 minutes you can take the hours and then divide the minutes by 60 like this:

Sub TestMe()

    Dim myTime As String

    myTime = "2 hours 30 minutes"
    Debug.Print Round(Split(myTime)(0) + Split(myTime)(2) / 60, 2)

    myTime = "2 hours 35 minutes"
    Debug.Print Round(Split(myTime)(0) + Split(myTime)(2) / 60, 2)

End Sub

You can even make a custom function for this:

Public Function TimeToDouble(myTime As String) As Double
    TimeToDouble = Round(Split(myTime)(0) + Split(myTime)(2) / 60, 2)
End Function

In excel, add three empty columns next to the time column. Then highlight the column and select "text to columns" select "space" as the deliminator. This will break the data up like this (without dots, they just help with format):

ABCD 2 hours . . 3 hours . . 1 hour 15 minutes 2 hours 30 minutes

each in their own column.

Then in a fifth column create the equation: =A1+C1/60

Format the column to the number of decimals you want and you'll get the following data:

E 2.00 3.00 1.25 2.50

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM