Results 1 to 7 of 7

Thread: over-night, used minutes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    492

    over-night, used minutes

    I have problem, update #3

    I need Check between IN and OUT time
    I use (Out - In) = NeedResult

    But result is "130", any method can result is "90" ?

    Please!

    Code:
    
    Private Sub Command1_Click()
    
    intIn = CInt("0900") '09:00
    intOut = CInt("1030") '10:30
    
    t = intOut - intIn '10:30 - 09:00 , 
    
    msgbox "used mins" & t
    
    End Sub
    Last edited by rpool; Nov 1st, 2009 at 09:01 AM.

  2. #2
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: used minutes

    But result is "130",
    Yes it will be 130, because the substraction will be done on numeric variables, because you are converted them to integers.
    To get the minutes i would recommend you to work with dates (time in this case), so you can use the datetime.datediff method to get the differences between two date stamps.

    Code:
      Dim vT1 As Variant, vT2 As Variant, vTR As Variant
      vT1 = CDate("09:00")
      vT2 = CDate("10:30")
      
      vTR = DateTime.DateDiff("n", vT1, vT2) 'the "n" stands for the minutes.
      Debug.Print vTR
    Last edited by Jim Davis; Nov 1st, 2009 at 08:51 AM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    492

    Re: used minutes

    I write this method, is look fine ?

    I try
    IN:2345, OUT:2359, result is "14", correct!
    IN:0000, OUT:0101, result is "61" , correct!

    if Over-Night: PROBLEM:
    IN:2345, OUT:0000 ,result "-1425" , WRONG, I known is used 15mins
    IN:2345, OUT:0015, result "-1410", wrong, I known is used 30mins

    result is wrong? can help me to fix !

    Please!


    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    
    Dim a As String
    Dim InTime As String
    a = tin.Text
    InTime = Mid(a, 1, 2) & ":" & Mid(a, 3, 2) & ":" & "00"
    
    Dim b As String
    Dim OutTime As String
    b = tout.Text
    OutTime = Mid(b, 1, 2) & ":" & Mid(b, 3, 2) & ":" & "00"
    
    
    Dim ra As String
    Dim rb As String
    
    
    ra = Format(InTime, "hh:mm:ss")
    rb = Format(OutTime, "hh:mm:ss")
    
    'DateTime.DateDiff
    'h Hour
    'n Minute
    's Second
    
    Dim rc As String
    
    rc = DateTime.DateDiff("n", ra, rb)
    
    MsgBox rc
    
    End Sub
    Last edited by rpool; Nov 1st, 2009 at 08:58 AM.

  4. #4
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: used minutes

    i would use variants instead of strings (ra, rb, rc) . i also recommended the CDate because it is just a somewhat compact way to convert strings/numbers to valid datestamps, while Fomat() is an extended method, with a lots of formating options. you just have to split the hour:min:sec with ":" as a delimiter and CDate() will interpret it as time, and will do the conversions just fine.

    IN:2345, OUT:0000 ,result "-1425" , WRONG, I known is used 15mins
    IN:2345, OUT:0015, result "-1410", wrong, I known is used 30mins
    Yes it will be. To avoid this to happen you might extend the datestamps with valid dates as well.
    Code:
      vT1 = CDate("11-01 23:45")
      vT2 = CDate("11-02 00:30")
    This will be an "anyyear",november 1 and november 2. you can save the month and day values when the timing starts, then at the end you have to inlude the actual month and day. you can also use the DateTime.DateValue(Now) to get the actual year.month.day in a string.

    Code:
      vT1 = CDate(DateTime.DateValue(Now) & "23:45")
    Last edited by Jim Davis; Nov 1st, 2009 at 09:07 AM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    492

    Re: over-night, used minutes

    Thanks, I rewrite it. Is Look fine ?
    But problem:
    (Q1) If over-night, is simple
    vT1 = anyMonth, anyDay,
    vT2 = anyMonth, anyDay + 1 ?

    (Q2) vT1 = CDate(InTime)
    Only accept InTime string = "23:45",
    NOT "23:45:00" ?


    Please!


    Code:
    Option Explicit
    
    
    Private Sub Command1_Click()
    
    Dim a As String
    a = Text1.Text
    Dim InTime As String
    InTime = Mid(a, 1, 2) & ":" & Mid(a, 3, 2) '& ":" & "00"
    
    Dim b As String
    b = Text2.Text
    Dim OutTime As String
    OutTime = Mid(b, 1, 2) & ":" & Mid(b, 3, 2) '& ":" & "00"
    
    Dim vT1 As Date
    Dim vT2 As Date
    Dim vTR As Long
    
    vT1 = CDate(InTime)
    vT2 = CDate(OutTime)
    
    vTR = DateTime.DateDiff("n", vT1, vT2) 'the "n" stands for the minutes.
      
        If vTR < 0 Then
    
            vT1 = CDate("11-01" & " " & CStr(InTime))
            vT2 = CDate("11-02" & " " & CStr(OutTime))
            vTR = DateTime.DateDiff("n", vT1, vT2) 'the "n" stands for the minutes.
      
        End If
      
    MsgBox vTR
    
    End Sub
    Last edited by rpool; Nov 1st, 2009 at 09:50 AM.

  6. #6
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: over-night, used minutes

    yes, it should work. but, can't you just store the datestamp with the timestamp? so you can compare the valid dates then. but it might not be necessary at all.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    492

    Re: over-night, used minutes

    Thanks, I Done it.
    I Not use "CDate", changed To used "DateSerial, TimeSerial"
    CDate maybe that something bug, I fixed ?

    I need only calc working time only, that date string is anyYear,anyMonth,anyDate OR need correct date /Today()?

    Please!


    Code:
    Option Explicit
    
    
    Private Sub Command1_Click()
    
    
    Dim a As String
    Dim b As String
    
    If t1.Text = "" Then t1.Text = "0000"
    a = t1.Text
    
    If t2.Text = "" Then t2.Text = "0800"
    b = t2.Text
    
    Dim vT1 As Date
    Dim vT2 As Date
    Dim vTR As Long
    
    vT1 = DateSerial(2009, 11, 1) + TimeSerial(CInt(Mid(a, 1, 2)), CInt(Mid(a, 3, 2)), 0)
    vT2 = DateSerial(2009, 11, 1) + TimeSerial(CInt(Mid(b, 1, 2)), CInt(Mid(b, 3, 2)), 0)
    vTR = DateTime.DateDiff("n", vT1, vT2)
        
    If vTR < 0 Then
        vT1 = DateSerial(2009, 11, 1) + TimeSerial(CInt(Mid(a, 1, 2)), CInt(Mid(a, 3, 2)), 0)
        vT2 = DateSerial(2009, 11, 2) + TimeSerial(CInt(Mid(b, 1, 2)), CInt(Mid(b, 3, 2)), 0)
        vTR = DateTime.DateDiff("n", vT1, vT2)
    End If
    
      
    t3.Text = vTR
    
    t4.Text = vTR \ 60 & ":" & vTR Mod 60
    
    
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width