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

  • Home
  • SEARCH
  • 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 9043845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:49:52+00:00 2026-06-16T10:49:52+00:00

My below code is doing kind of duration calculation, but at run time it

  • 0

My below code is doing kind of duration calculation, but at run time it is throwing an exception that type mismatch, although both of the parameter has date values within them.So any suggestion where the bug is?

Code:

Function TimeSpan(dt1,dt2)
Dim dtTemp

objExcel1.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 ;)
    Msgbox("DT2:" & dt2 & "DT1:" & dt1)
    TimeSpan = objExcel1.Application.WorksheetFunction.Text((dt2 - dt1), "[h]:mm:ss")'"dd:hh:mm:ss"

objExcel1.Application.ScreenUpdating = True
End Function

Date values to the parameters passed by the Calle

Date error

@Tomalak I have updated the code as per you and now getting below error.

CODE

Function TimeSpan(dt1, dt2)
    If Not (IsDate(dt1) And IsDate(dt2)) Then

        TimeSpan = "00:00:00"

    ElseIf dt2 < dt1 Then

        TimeSpan = TimeSpan(dt2, dt1)

    Else

        MsgBox((CDate(dt1) - CDate(dt2)))
        TimeSpan = objExcel1.Application.WorksheetFunction.Text( (CDate(dt1) - CDate(dt2)), "[h]:mm:ss" )

    End If  

End Function

Msgbox Displaying:

enter image description here

New Error

  • 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-16T10:49:57+00:00Added an answer on June 16, 2026 at 10:49 am

    There is a function DateDiff() available, which makes life simpler:

     Function TimeSpan(dt1, dt2)
    
        If Not (IsDate(dt1) And IsDate(dt2)) Then
            TimeSpan = "00:00:00"
        Else
           TimeSpan = Abs(DateDiff("s", dt2, dt1)) / 3600
             TimeSpan = TimeSpan / 24
        End If
    
     MsgBox (TimeSpan)
    

    The result is in days, re-calculated from seconds to achieve the needed (minutes and seconds) accuracy and compatibility with date formats.

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

Sidebar

Related Questions

I'm doing a homework project that requires this: Below you will find the code
I've run into a rather interesting problem. The code below generates an array that,
I'm using the line of code below but it doesn't seem to be doing
Can somebody explain to me what the below code is doing. before and after
In C# 3.0, I'm doing the code below to declare a DateTime property and
My code below won't compile. What am i doing wrong? I'm basically trying to
Below code not work, but it's work fine for jsf1.2. Now the framework is
below code is my databasehandler class i got it from a tutorial. Beside that
please tell me what is below code doing? is it creating array with two
I was doing some unit testing on code that could throw a number of

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.