Home > front end >  How to change date format to hour only
How to change date format to hour only

Time:09-21

i'm trying to calculate the duration of two cells using vba.

I defined t1 as date and t2 as date in my code.

Duration will be t2-t1 and will be presented on another cell.

The format for t1 and t2 is like this : dd/mm/yy hh:mm

I want the duration to be an integer that represent the hour.

But i don't know how to separate the format to get only the time.

CodePudding user response:

If you mean you want to calculate how many hours between the two values, you just need to multiple the answer by 24. Eg in A1 I have 01/09/2021 9:00:00 AM and in cell B1 I have 01/09/2021 2:00:00 PM. In the following code, hours = 5.

Sub theTime()

Dim t1 As Date
Dim t2 As Date
Dim hours As Integer
t1 = Range("A1").Value
t2 = Range("B1").Value
hours = (t2 - t1) * 24

End Sub
  • Related