ѧ蹷㹡äӹdzҤҤᵡҧѹͧ (Time)

Category »  VB 6/VB.Net
: Webmaster 17/3/2552   : 14:31
(ҹ : 32857) 
ѹԧͧµͺӶФѺ ... §¹͸Ժ١ѹ ¤§ ʧҷ 仢ͧǤԴѹҧ йաҧ˹觷ͧʴ繶֧ "ԸաäԴ Algorithm" Ҩ (дѺ٧) ˹ ԸաäԴ ѹԴἡᵡҧѹ Ѩغѹ硹ѡ֡ҷ診 ѧШ 繨ӹǹҡ١֡ ١֡¹ ... ҧ BASIC 觷ҹ¹Ѻ BASIC Ѻ BASIC 鹰ҹ㹡ҹ Ѻ ҡҧ ѹ ... ... ѹ BASIC ѹ ... ¤͹ѹ ѹ ǹ ǧҤѺͧ ֡͡Ҵ¤㹵ͧ٧ ʴԹաѺͧ (True) ͧ ͧ 2 ˹ MSN ... ҧһͺѭѧ㨾 55555+ (ǹͧ˭ԧ֧軷ҹ ¹ ͺ Dos_Network ù 件֧˹ǤҺ ... ͧԭҺѵù ԭҺص )
ѡ
  1. Ҥᵡҧѹͧ 2 ¤ (Ϳѧ) DateDiff ͡дѺԹҷ
  2. Ҥᵡҧѹͧӹǹ ô¨ӹǹ 3600 Թҷ (60 ҷ x 60 Թҷ)
  3. Ҥᵡҧѹͧӹǹҷ ź͡ҡӹǹҧѹ͹ (ԴԹҷչФѺ) ô¨ӹǹ 60 Թҷ (1 ҷ ҡѺ 60 Թҷ)
  4. ҤᵡҧѹдѺԹҷ ź͡ҡӹǹҧѹ (ԴԹҷ) ź͡¨ӹǹҷշҧѹ (дѺԹҷ)
Ǣͧ
ѧ蹷㹡äӹdzҤҤᵡҧҧѹ͹ 2
ǹŴ
ǹŴ Source Code Ѻ MS Visual Basic 6.0 - Service Pack 6
 ǹŴ Visual Basic 6.0 SP5: Run-Time Redistribution Pack
 ǹŴ Microsoft Data Access Object (MDAC) Jet 4.0 Update
 ǹŴ Microsoft Visual Basic Service Pack 6
鹡кǹ÷ӧҹ

Project --> Components ...


Design Time


سѵԢͧ dtPicker


Run-Time


Ѿҡäӹdz

Ҵ鴡ѹ ...

Option Explicit

Private Sub cmdCalDiffTime_Click()
Dim ResultTime As String
    ' ¡ѧѧ CalTime 觤  ش
    '  觤ҡѺ (Return) Ẻ String ... سҡ
    ResultTime = CalTime(dtpStartTime, dtpEndTime)
    
    ' 觤ҧҡѺ ... ʴҡäӹdz ͧӡʴ
    If ResultTime <> "" Then MsgBox "ӹǹҷҧѹ : " & _
        ResultTime, vbOKOnly + vbInformation, "§ҹ"
End Sub

Private Sub Form_Load()
    ' 駿觡ҧҾ ... ͧ \  õѴ
    ' äӹdzẺǡҡ / ... ФѺͧ ()
    Me.Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
    
    ' ˹㹡÷ͺ
    dtpStartTime.Value = "8:30:23"
    dtpEndTime.Value = "12:46:53"
End Sub

Public Function CalTime(StartTime As Date, EndTime As Date) As String
On Error GoTo ErrHandler

Dim cHH As Long
Dim cMM As Long
Dim cSS As Long

' ͺҡ͹鹵ͧ¡ҷش
If StartTime = EndTime Then
    ' ҡѹͧӹdzա ...  55555+
    CalTime = "0:00:00"
    Exit Function
    
' ҡ  Error Һ
ElseIf StartTime > EndTime Then
    MsgBox "鹨ҡоͧ ... .", _
                    vbOKOnly + vbInformation, "§ҹԴҴ"
    ' ׹ҧҡѺ ...  ҵͧ駡Ѻҷӧҹ ...
    ' Ш (Function)
    CalTime = ""
    Exit Function
End If
  
' ===============================================================================
' ѡâͧҤҤᵡҧͧ
' ҤҤᵡҧдѺӹǹͧԹҷշ͡ҡ͹
' Ǥ¤ԴѴ͡ҡӹǹ ҷ  Թҷ ҧѹ
' 1  ҡѺ 3600 Թҷ
' 1 ҷ ҡѺ 60 Թҷ
Dim SecInMinute As Integer
Dim SecInHour As Integer
SecInMinute = 60    ' ӹǹ 1 ҷҡѺ 60 Թҷ
SecInHour = 3600    ' Ҩҡӹǹ 60 ҷ 1  ٳҡѺ 60 Թҷйҷ ѧ 1  = 3600 Թҷ
' ===============================================================================

' Ҩӹǹ (Hour) ... õѴ ( \ )  ... ӧҹǡҡ ( / )
' á˹ԴẺŢӹǹ (Integer  Long) ѹŢȹǤѺ
' Ҥᵡҧͧӹǹ ¹ѺԹҷ ҡõѴɴ (ӹǹҷ x ӹǹԹҷ)
' 蹤 ҷ \ (60 ҷ x 60 Թҷ) ... 1 ҡѺ 3600 Թҷ
cHH = DateDiff("s", StartTime, EndTime) \ SecInHour

' Ҩӹǹҷ (Minute) ...  ͧǧ١¹ФѺ
' ǧ դӤѭ٧ͧ + - * / ... 索Ҵ
' ᵡҧͧӹǹҷ ҷź͡ҡӹǹԹҷ
' ӹǹԹҷշ ź͡ҡҨӹǹҧѹ͹ ( x Թҷ)
' ǤõѴɴ 60 ...  1 ҷ = 60 Թҷ
cMM = (DateDiff("s", StartTime, EndTime) - (cHH * SecInHour)) \ SecInMinute

' ͡Թҷ (Second)
' ӹǹԹҷշҧѹ - ӹǹҧѹ - ӹǹҷշҧѹ
' ӹǹҧѹ Ѻ  ӹǹҷշҧѹ ҤԴдѺԹҷչФѺ ... ͧ
cSS = DateDiff("s", StartTime, EndTime) - (cHH * SecInHour) - (cMM * SecInMinute)

' ׹ҡѺѧ (Return ҡѺ String)
CalTime = cHH & ":" & cMM & ":" & cSS

ExitProc:
    Exit Function

ErrHandler:
    MsgBox Err.Number & vbCrLf & Err.Description, vbOKOnly + vbInformation, "§ҹԴҴ"
    Resume Next
End Function
Conclusion:
ª礧ͧͧäӹdzҢͧ÷ӧҹ ÷ӧҹǧ (Over Time - OT) ѡФѺ ͧ ... 觨ҡ͹繡äӹdzͧͧѹ 觡äӹdzҡҹ ЪҨӹǹҢͧ÷ӧҹѹҧҡ繹ѡ͡Ѻ ... ͧ