dcsimg
Results 1 to 12 of 12

Thread: [VB6] Time Diff

  1. #1

    Thread Starter
    New Member GywGod133's Avatar
    Join Date
    Jul 2015
    Location
    Quezon City, Philippines
    Posts
    6

    [VB6] Time Diff

    Hello fellas!

    I need help for this...

    How to get the Hours, Minutes, and Seconds? in Time In and Time Out

    Like this:
    *******
    1:30:20 AM and 12:00:20 PM = [HOURS, MINUTES, SECONDS]

    1:30:20 AM = Time In
    12:00:20 PM = Time Out
    *******

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: [VB6] Time Diff

    Do you mean something like this?

    Code:
    Option Explicit
    
    Private Sub Main()
        Debug.Print """"; Format$(#1:30:20 AM#, "\[Hh, Nn, Ss]"); """"
        Debug.Print """"; Format$(#12:00:20 PM#, "\[Hh, Nn, Ss]"); """"
    End Sub
    Code:
    "[01, 30, 20]"
    "[12, 00, 20]"
    If yes then see the documentation of the Format$ function and its See Also link.

    If you want to extract the hour, minute and second component of a time value instead, then see the Hour, Minute and Second functions.



    EDIT

    You might also want to check out the DateDiff function.
    Last edited by Bonnie West; Aug 23rd, 2015 at 02:51 PM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: [VB6] Time Diff

    maybe this is what you want ?
    Code:
    Private Sub Command1_Click()
        Dim Entering As Double
        Dim Leaving As Double
        Dim Present As Date
        Entering = CDbl(#1:30:20 AM#)
        Leaving = CDbl(#12:00:20 PM#)
        Present = CDate(Leaving - Entering)
        Print Present
    End Sub
    do not put off till tomorrow what you can put off forever

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,710

    Re: [VB6] Time Diff

    Simple subtraction or addition with Date typed data isn't really a defined operation, though VB will let you get away with it. However you can end up with garbage results pretty easily as soon as you move across a day boundary or have a negative result.

    This is less of a problem if you absolutely know you have no negative differences or differences of 24 hours or more, but even then it probably isn't a good practice.

    Getting it right can be tricky, and I might still have "negative flaws" here but it looks good.

    Code:
    Option Explicit
    
    Private Function TDiff(ByVal Later As Date, ByVal Earlier As Date) As Double
        TDiff = CDbl(Later) - CDbl(Earlier)
    End Function
    
    Private Function FormatTDiff(ByVal TDiff As Double) As String
        Dim TDiffSgn As Integer
    
        TDiffSgn = Sgn(TDiff)
        TDiff = Abs(TDiff)
        If TDiffSgn < 0 Then FormatTDiff = "-"
        If TDiff >= 1# Then
            FormatTDiff = FormatTDiff & CStr(Int(TDiff)) & "d, "
            TDiff = TDiff - Int(TDiff)
        End If
        FormatTDiff = FormatTDiff & Format$(CDate(TDiff), "Hh:Nn:Ss")
    End Function
    
    Private Sub cmdDifference_Click()
        lblDiff.Caption = FormatTDiff(TDiff(dtpDOut.Value + dtpTOut.Value, _
                                            dtpDIn.Value + dtpTIn.Value))
    End Sub
    Name:  sshot1.png
Views: 1468
Size:  9.4 KB

    Name:  sshot2.png
Views: 1395
Size:  9.6 KB

    Check it over though, I still feel uneasy about the negative differences.
    Attached Files Attached Files

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,531

    Re: [VB6] Time Diff

    Assuming the 2nd time is never less than the 1st time, and these are on the same day, then this should work easily enough, no?
    Code:
    dblSecond = DateDiff("s", CDate([time1]), CDate([time2]))
    Msgbox Format(DateAdd("s", dblSecond, Date), "hh nn ss")
    Last edited by LaVolpe; Aug 23rd, 2015 at 04:04 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,710

    Re: [VB6] Time Diff

    Looks like it should work, but I think DateDiff() returns a Long.

  7. #7
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,603

    Re: [VB6] Time Diff

    Quote Originally Posted by dilettante View Post
    Simple subtraction or addition with Date typed data isn't really a defined operation, though VB will let you get away with it. However you can end up with garbage results pretty easily as soon as you move across a day boundary or have a negative result.
    Care to give some examples?

    Keeping transitivity of the (positively) defined Date + Double operation we have Date(x) +/- Double(y) = Date(z) => Date(x) +/- Date(z) = Double(y)

    CDate(-2.5) is a valid Date too.

    @GywGod133: I'm using this helper function for duration output: FormatDuration(ClockOut-ClockIn)

    Code:
    Public Function FormatDuration(ByVal dDate As Date) As String
        Dim lHours          As Long
        
        lHours = DateDiff("h", 0, dDate)
        FormatDuration = Format$(lHours, "0") & Mid$(FormatTime(DateAdd("h", -lHours, dDate)), 3)
    End Function
    
    Public Function FormatTime(ByVal dDate As Date) As String
        FormatTime = Format$(dDate, IIf(Second(dDate) <> 0, "hh:mm:ss", "hh:mm"))
    End Function
    Keep in mind that duration can get above 24 hours so you cannot use std Format function on a Date variable as is.

    cheers,
    </wqw>
    Last edited by wqweto; Aug 25th, 2015 at 03:29 AM.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,710

    Re: [VB6] Time Diff

    Quote Originally Posted by wqweto View Post
    Care to give some examples?
    I was referring to the difference in results you see when conventional formatting is applied. Everything might seem fine as long as you have positive values less than 24 hours, but then it falls apart.

    Code:
        Dim D As Date
    
        D = #1/1/2000 12:00:00 PM# - #1/2/2000 11:00:00 AM#
        Debug.Print "Looks ok: "; Format$(D, "Hh:Nn:Ss")
        D = #1/2/2000 12:00:00 PM# - #1/1/2000 12:00:00 PM#
        Debug.Print "Looks wrong: "; Format$(D, "Hh:Nn:Ss")
        D = #1/1/2000 12:00:00 PM# - #1/2/2000 1:00:00 PM#
        Debug.Print "Looks wrong: "; Format$(D, "Hh:Nn:Ss")
    
    Looks ok: 23:00:00
    Looks wrong: 00:00:00
    Looks wrong: 01:00:00

  9. #9
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,603

    Re: [VB6] Time Diff

    I see, things get wrong real quick. Btw, I noticed that Debug.Print TypeName(Abs(CDate(-2.5))) returns Date which is news for me that Abs has built-in support for Date datatype.

    I frankly never intended FormatDuration to be used with "negative" durations and I'm bit surprised it works ok as is with your input
    Code:
    Looks ok: 22:00 '--- Edit: hmm, wrong
    Looks wrong: 24:00
    Looks wrong: -23:00 '--- Edit2: wrong again
    cheers,
    </wqw>
    Last edited by wqweto; Aug 25th, 2015 at 01:41 PM.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,710

    Re: [VB6] Time Diff

    In the end all we can conclude is that Date type isn't meant for intervals. An alternative might be to just use DateDiff("s", d1, d2), work with the Long seconds value, and write a formatting function to create a display format String when required.

  11. #11
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,603

    Re: [VB6] Time Diff

    Ok, so DateDiff failed me badly. Here is a "manual" calculation of hours difference
    Code:
    Public Function FormatDuration(ByVal dDate As Date) As String
        Dim lHours          As Long
        
    '    lHours = DateDiff("h", 0, dDate)
        lHours = Round(dDate * 24& * 60 * 60) \ (60& * 60)
        FormatDuration = Format$(lHours, "0") & Mid$(FormatTime(DateAdd("h", -lHours, dDate)), 3)
    End Function
    ... which gets correct output IMO
    Code:
    Looks ok: -23:00
    Looks wrong: 24:00
    Looks wrong: -25:00
    ... provided that we accept that a duration can have a sign.

    cheers,
    </wqw>

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,710

    Re: [VB6] Time Diff

    I'd probably live with seconds as Long type for intervals. Lots you can do with them including adding them back to a Date where desired via DateAdd().

    Something like:

    Code:
    Option Explicit
    
    Private Function Fmt(ByVal Secs As Long) As String
        'Result suppresses 0 value in days position.
        Dim AbsSecs As Long
    
        AbsSecs = Abs(Secs)
        Fmt = Format$(AbsSecs \ 3600 Mod 24, "00") & ":" _
            & Format$(AbsSecs \ 60 Mod 60, "00") & ":" _
            & Format$(AbsSecs Mod 60, "00")
        If AbsSecs >= 86400 Then
            Fmt = CStr(AbsSecs \ 86400) & "d " & Fmt
        End If
        If Secs < 0 Then
            Fmt = "-" & Fmt
        End If
    End Function
    
    Private Sub Form_Load()
        Dim D1 As Date, D2 As Date
    
        D1 = #1/5/2000 12:00:00 PM#
        D2 = #1/1/2000 12:00:00 PM#
        Debug.Print Fmt(DateDiff("s", D1, D2))
    
        D1 = #1/1/2000 1:00:00 PM#
        D2 = #1/1/2000 12:00:00 PM#
        Debug.Print Fmt(DateDiff("s", D1, D2))
    
        D1 = #1/1/2000 12:00:00 PM#
        D2 = #1/1/2000 1:00:00 PM#
        Debug.Print Fmt(DateDiff("s", D1, D2))
    
        D1 = #1/1/1999 12:00:00 PM#
        D2 = #1/1/2000 1:15:45 PM#
        Debug.Print Fmt(DateDiff("s", D1, D2))
    End Sub
    Code:
    -4d 00:00:00
    -01:00:00
    01:00:00
    365d 01:15:45
    Last edited by dilettante; Aug 25th, 2015 at 02:09 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width