Arup Rakshit
Arup Rakshit

Reputation: 118261

Date difference in Day-Hour-Minutes format

Can we get the two date values difference using VBScript in Day-Hour-Minutes format?

Upvotes: 1

Views: 8137

Answers (1)

Nahum
Nahum

Reputation: 7197

here is a HH::MM:SS example changing it to DD::HH::MM should be easy

ASP FAQ - Date time routines manipulation

<% 
    Function TimeSpan(dt1, dt2) 
        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 
        minutes = minutes mod 60 
        seconds = seconds mod 60 

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

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

    d1 = "2002-03-27 9:20:25 AM" 
    d2 = "2002-03-27 9:20:45 AM" 

    Response.Write TimeSpan(d1, d2) 
%>

something like:( im not sure, I don't know vbscript)

<% 
    Function TimeSpan(dt1, dt2) 
        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 
        hours   = hours   mod 24 

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

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

    d1 = "2002-03-27 9:20:25 AM" 
    d2 = "2002-03-27 9:20:45 AM" 

    Response.Write TimeSpan(d1, d2) 
%>

Upvotes: 9

Related Questions