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

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • Why do you need such a complicated function when the *elapsed time* can be found quite easily? And please confirm, that you are using **VBSCRIPT** or **VBA** which are two different things. – bonCodigo Dec 12 '12 at 09:58
  • I am using VBScript Bon... I am not educated with VBA.... – Arup Rakshit Dec 12 '12 at 12:02

2 Answers2

1

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.

Community
  • 1
  • 1
bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • Can you help me in my below post please... that was really painful for me to implement ? - http://stackoverflow.com/questions/13819708/sparse-matrix-creation-using-vbscript#comment19033369_13819708 – Arup Rakshit Dec 12 '12 at 12:22
  • Let me give it a try? But you do understand the concept on the above answer? – bonCodigo Dec 12 '12 at 12:43
  • 1
    BTW looking at your comments, it seems KB's solution is working for you apart from a minor am/pm issue. Format that into 24 hour format, so you will not get such. Let me know if you still require changing my code into VBScript? :) – bonCodigo Dec 12 '12 at 12:53
  • Yes, I am done! with KB's solution.. Please help me on my above mentioned post..Bon... I am really...in middle of the ocean.. My code is too much time taking one...thus taking you guys help to make it optimized one. :-) – Arup Rakshit Dec 12 '12 at 13:25
  • If possible you can advice on KB's code,where you want me to put such 24 hour formatting. :-) – Arup Rakshit Dec 12 '12 at 13:29
  • If you vote me up for the logic, perhaps may be ;) it is as painful as for anyone to try converting into VB Script – bonCodigo Dec 12 '12 at 13:50
  • Hi Bon Can you help me in the below post? http://stackoverflow.com/questions/13823912/to-move-the-cell-values-in-a-group-from-right-to-left-if-any-group-of-cells-are/13853950#comment19077100_13853950 – Arup Rakshit Dec 13 '12 at 18:49
  • @VBSlover sorry your response to my above answer was very poor. How do you measure people's effort put on you in the first place? – bonCodigo Dec 13 '12 at 19:29
  • Sorry for that, today i implemented your code and tested also. it was fine,but forgot to change my vote dear. I am doing it right away. – Arup Rakshit Dec 13 '12 at 19:38
  • This solution will fail when the timespan is more then the number of days in january (31), if that will never happen it is fine, otherwise you better use my code or something similar. – K_B Dec 14 '12 at 13:35
1

Be aware I'm more used to write VBA, so you might need to tweak here and there.

Alternatively you could just subtract the two dates from eachother as numerical value:

Dim dblDateDiff as Double
dblDateDiff = Abs(dt2 - dt1)

Now the Timespan would be (dont use "d" as that would not include months and years that could have passed):

Timespan = Int(dblDateDiff) & ":" & Hour(dblDateDiff) & ":" & Minute(dblDateDiff)

If the direction (positive or negative) of the Timespan is relevant you could change the last line into:

Timespan = Sgn(dblDateDiff) * Int(dblDateDiff) & ":" & Hour(dblDateDiff) & ":" & Minute(dblDateDiff)

For your time formatting issue either:

  1. Set the number format of the output cell to Text, or
  2. Add a single quote in front of the rest of the string:

     Timespan = "'" & Sgn(dblDateDiff) * Int(dblDateDiff) & ":" & Hour(dblDateDiff) & ":" & Minute(dblDateDiff)
    
K_B
  • 3,668
  • 1
  • 19
  • 29
  • Can you help me in my below post please... that was really painful for me to implement ? http://stackoverflow.com/questions/13819708/sparse-matrix-creation-using-vbscript#comment19033369_13819708 – Arup Rakshit Dec 12 '12 at 12:21
  • Did my answer work for you? Is the concept clear for you? If not please let me/us know what is not working. In case an answer provided did work for you then tick the V shaped mark to the left of the answer post (or that of @bonCodigo in case his answer worked for you) so all may know you dont need further assistance with this particular question. – K_B Dec 12 '12 at 12:31
  • Yes I have tried your one... And it is producing the output a bit better than previous one. But with the duration i also got the the output as : D1="9/11/2012 1:42:59 AM" and D2="9/4/2012 6:09:15 AM",but the output is : "6:19:33 AM".. I do want that duration part only,not any AM or PM post the duration.Please advice how to remove that. – Arup Rakshit Dec 12 '12 at 12:33
  • I am not understanding your comment, have you applied my script and does that give you this problem (I think that is not possible), or have you got this AM,PM problem with another solution? – K_B Dec 12 '12 at 12:41
  • Yes, I tried your one. In my spreadsheet the result is showing AM Or PM with the correct durations... – Arup Rakshit Dec 12 '12 at 12:43
  • 1
    What could happen is that Excel "thinks" the answer day:hour:minute is actually hour:minute:second, to avoid this you will have to manually set the number format of the cells you write the answer to Text. Do that BEFORE you actaully write the answer to it as otherwise Excel will already have converted it. Alternatively you could add a single quote at the start of your string, with that Excel will not convert the output to a time value! – K_B Dec 12 '12 at 12:46
  • There are 500 columns out of which 300 will be updating by the date function.So manually to change such columns are impossible, as the columns are also not fixed..it is dynamic. So suggest me in the code where to change if possible. :-) – Arup Rakshit Dec 12 '12 at 13:22
  • if not manually then programmatically. And the coding alterative is in my answer (I updated it 40 minutes ago) at the end. – K_B Dec 12 '12 at 13:30
  • Yes...due to browser issue i haven't seen that. Sorry to bother you in that case.:-) – Arup Rakshit Dec 12 '12 at 13:33
  • No problem, if that works for you please mark as answered. Good luck with your other questions as well! – K_B Dec 12 '12 at 13:34
  • Yes..Sure I will.Please help me on my another post,as i just pasted here above. – Arup Rakshit Dec 12 '12 at 13:43
  • http://stackoverflow.com/questions/797200/duration-vbscript-vbs-function as an reference – Arup Rakshit Dec 16 '12 at 12:52