Excel VBA: How To Calculate Days, Hour & Minute Between 2 Dates

kashif.special2005

Active Member
Joined
Oct 26, 2009
Messages
443
Hi,

I have 2 dates with time stamp in an Array variable and I want to calculate Days, Hours And Minutes between the 2 dates,

Below is the example:

Start Date: 1/1/2021 12:24:00 PM
End Date: 12/31/2021 2:15:02 PM

And the result should be as below.

Result: 364 Days 1 Hour 51 Minutes

Thanks in advance for all the help.

Thanks
Kashif
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi there

Will the below formula route work? You would need to look at your cell formatting though....

Book1
AB
1Start01/01/2021 12:24
2End31/12/2021 14:15
3Total364 Days 1 Hour 51 Minutes
Sheet1
Cell Formulas
RangeFormula
B3B3=INT(B2-B1)&" Days "&HOUR(MOD(B2-B1,1))&" Hour "&MINUTE(MOD(B2-B1,1))&" Minutes"
 
Upvote 0
Try this code :
VBA Code:
Sub Date_Dif()

    Dim D1 As Date
    Dim D2 As Date
    D1 = Range("A1").Value
    D2 = Range("A2").Value

    Dim diff As Date
    diff = D2 - D1

    ActiveSheet.Range("A3").Value = _
               CLng(Int(diff)) & " days " & _
               Hour(diff) & " hours " & _
               Minute(diff) & " minutes " & _
               Second(diff) & " seconds"
End Sub
 
Upvote 0
Thank you so much all of you for your precious time to solve this question.

I am using Phuoc method to solve this issue, below is the code that I am using to solve this issue.

Code:
Sub Calculate_DaysHoursMinute_From_TwoDates()

    Dim sDays As String, sHour As String, sMinute As String
    Dim vMin As Date, vMax As Date
    Dim vResult As Variant
    
    vMin = Range("A2").Value  'StartDate
    vMax = Range("B2").Value  'EndDate
    
    'INT(B2-A2)&" day(s) "&TEXT(B2-A2,"h""  Hour(s) ""mm"" Minute(s)""")
    Debug.Print Int(vMax - vMin) & " day(s) " & Format(vMax - vMin, "h"" Hour(s) ""m"" Minute(s)""")
    
End Sub

Thank you so much again all of you.

Thanks
Kashif
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top