Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8983139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:45:45+00:00 2026-06-15T20:45:45+00:00

I am using the below code to show the date difference in Day:Hour:Minute format.

  • 0

I am using the below code to show the date difference in Day:Hour:Minute format.

 Function TimeSpan(dt1, dt2) 

    Dim seconds,minutes,hours,days

    If (isDate(dt1) And IsDate(dt2)) = false Then 
        TimeSpan = "00:00:00" 
        Exit Function 
    End If 

    seconds = Abs(DateDiff("S", dt1, dt2)) 
    minutes = seconds \ 60 
    hours = minutes \ 60 
    days  = hours \ 24
    minutes = minutes mod 60 
    seconds = seconds mod 60 
    days    = days    mod 24 

    if len(hours) = 1 then hours = "0" & hours 

    TimeSpan = days& ":" & _ 
        RIGHT("00" & hours , 2) & ":" & _ 
        RIGHT("00" & minutes, 2) 
End Function 

But it is not producing expected values for some cases.

  D1=#9/24/2012  8:09:15 AM# and D2=#9/25/2012  8:09:15 AM# gives correct data like 1:24:00 whereas below are producing error when working with VBScript and Excel.
  D1=#9/5/2012  8:45:43 AM# and D2=#9/25/2012  8:45:43 AM# result=0.888888888888889
  D1=#9/6/2012  8:29:34 AM# and D2=#9/17/2012  8:59:36 AM# result=0.503125

Can you explain why so?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:45:47+00:00Added an answer on June 15, 2026 at 8:45 pm

    Try my answer from an earlier post in your UDF as the following: This answer is in VBA

    Please declare all variables and force yourself to declare by adding option explicit 🙂

    option explicit
    Function TimeSpan(dt1 As Date, dt2 As Date) As String
    Dim dtTemp As Date
    
        Application.ScreenUpdating = False
            If (IsDate(dt1) And IsDate(dt2)) = False Then
                TimeSpan = "00:00:00"
                Exit Function
            End If
    
            If dt2 < dt1 Then
                dtTemp = dt2
                dt2 = dt1
                dt1 = dt2
            End If
            '-- since you only had days, I have put up to days here. 
            '-- if you require months, years you may use yy:mm:dd:hh:mm:ss
            '-- which is pretty self-explainatory ;)
            TimeSpan = Application.WorksheetFunction.Text((dt2 - dt1), "dd:hh:mm:ss")
    
        Application.ScreenUpdating = False
    End Function
    

    UDF Output:

    enter image description here

    But I really suggest you to use Excel sheet functions if you have the freedom and possibility to do so.


    If date difference is more than 31 days

    Then use the solution as per this article
    Incorporate the DateDiff to the UDF.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below code to show jquery dialog, its working perfectly, Sub OpenDialog(ByVal
In past, I am using Listview and using below code can show a particular
I am using the code below to show/hide a navigation menu on click of
Using the below code I am unable to show the open/save as file dialog:
i am using below code to show google map in asp.net MVC2: <script type=text/javascript
I am using below code to upload excel and INSERT in mysql in php
I am using below code to get profile image of friends using Resfb. I
I am using below code to generate photo gallery from a folder. How can
I'm using below code to check some form fields and render datatable table on
i am using below code to change the the font type of text view.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.