【VB】“HH:MM:SS”格式的时间的计算
原理很简单。转换成秒。然后进行计算(加减乘除与或非、左移右移等)。最后再转换回HH:MM:SS的格式。看这界面!多直观!还支持16进制!
注意我用的是VB6哈。不是.NET
BIN下载:
SRC下载:
顺带我把源代码放出来。一览无余。VERSION 5.00
Begin VB.Form frmTimeCalc
BorderStyle = 1'Fixed Single
Caption = "时间计算器"
ClientHeight = 495
ClientLeft = 45
ClientTop = 375
ClientWidth = 6495
Icon = "frmTimeCalc.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 33
ScaleMode = 3'Pixel
ScaleWidth = 433
StartUpPosition = 3'窗口缺省
Begin VB.ComboBox CbOperator
Height = 300
ItemData = "frmTimeCalc.frx":000C
Left = 2040
List = "frmTimeCalc.frx":002B
Style = 2'Dropdown List
TabIndex = 4
Top = 120
Width = 855
End
Begin VB.TextBox txtResult
BackColor = &H8000000F&
Height = 270
Left = 5400
Locked = -1'True
TabIndex = 3
Text = "00:00:00"
Top = 120
Width = 975
End
Begin VB.CommandButton Calc
Caption = "="
Height = 255
Left = 4920
TabIndex = 2
Top = 120
Width = 375
End
Begin VB.TextBox txtTime2
Height = 270
Left = 3000
TabIndex = 1
Text = "00:00:00"
Top = 120
Width = 1815
End
Begin VB.TextBox txtTime1
Height = 270
Left = 120
TabIndex = 0
Text = "00:00:00"
Top = 120
Width = 1815
End
End
Attribute VB_Name = "frmTimeCalc"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'作者:0xAA55
'论坛:http://www.0xaa55.com/
'版权所有(C) 2013-2014 技术宅的结界
'请保留原作者信息否则视为侵权
Option Explicit
'为了保证我们不在地球的时候也能用上这个程序,我把这些常数弄了一下
Private Const SecondsPerMinute As Long = 60 '每分钟秒数
Private Const MinutesPerHour As Long = 60 '每小时分钟数
Private Const HoursPerDay As Long = 24 '每天的小时数
Private Const SecondsPerHour As Long = SecondsPerMinute * MinutesPerHour '每小时的秒数
Private Const SecondsPerDay As Long = SecondsPerHour * HoursPerDay '每天的秒数
'TimeToSeconds:
'将描述时间的字符串转换成以秒计数的数值。字符串的格式是 小时:分钟:秒
Function TimeToSeconds(ByVal TimeString As String) As Long
On Error GoTo ErrHandler
Dim Hours&, Minutes&, Seconds&, Strings$()
Strings = Split(TimeString, ":")
Hours = CLng(Replace(Strings(0), "0x", "&H"))
Minutes = CLng(Replace(Strings(1), "0x", "&H"))
Seconds = CLng(Replace(Strings(2), "0x", "&H"))
TimeToSeconds = Seconds + Minutes * SecondsPerMinute + Hours * SecondsPerHour
Exit Function
ErrHandler:
TimeToSeconds = -1
End Function
'SecondsToTime:
'将描述时间的以秒计数的数值转换成字符串。字符串的格式是 小时:分钟:秒
Function SecondsToTime(ByVal TimeSeconds As Long) As String
Dim Hours&, Minutes&, Seconds&, Sign As String
If TimeSeconds < 0 Then
TimeSeconds = -TimeSeconds
Sign = "-"
End If
Seconds = TimeSeconds Mod SecondsPerMinute
Minutes = (TimeSeconds \ SecondsPerMinute) Mod MinutesPerHour
Hours = (TimeSeconds \ SecondsPerHour) Mod HoursPerDay
SecondsToTime = Sign & Format$(Hours, "00") & ":" & Format$(Minutes, "00") & ":" & Format$(Seconds, "00")
End Function
Private Sub Calc_Click()
On Error Resume Next
'先全部转换成秒
Dim Seconds1 As Long, Seconds2 As Long
Seconds1 = TimeToSeconds(txtTime1.Text)
Seconds2 = TimeToSeconds(txtTime2.Text)
If Seconds1 = -1 Or Seconds2 = -1 Then
MsgBox "时间格式不对。请按照“时:分:秒”的方式输入。", vbCritical, "无法计算。"
Exit Sub
End If
Dim ResultSeconds As Long
'然后进行计算。支持⑨种计算
Select Case CbOperator.ListIndex
Case 0 '加
ResultSeconds = (Seconds1 + Seconds2) Mod SecondsPerDay
Case 1 '减
ResultSeconds = (Seconds1 - Seconds2) Mod SecondsPerDay
Case 2 '乘
ResultSeconds = (Seconds1 * Seconds2) Mod SecondsPerDay
Case 3 '除
ResultSeconds = (Seconds1 \ Seconds2) Mod SecondsPerDay
Case 4 '与
ResultSeconds = (Seconds1 And Seconds2) Mod SecondsPerDay
Case 5 '或
ResultSeconds = (Seconds1 Or Seconds2) Mod SecondsPerDay
Case 6 '异或
ResultSeconds = (Seconds1 Xor Seconds2) Mod SecondsPerDay
Case 7 '左移
ResultSeconds = (Seconds1 * (2 ^ Seconds2)) Mod SecondsPerDay
Case 8 '右移
ResultSeconds = (Seconds1 \ (2 ^ Seconds2)) Mod SecondsPerDay
End Select
txtResult.Text = SecondsToTime(ResultSeconds)
End Sub
Private Sub Form_Load()
CbOperator.ListIndex = 0
End Sub vb学习中,非常有收获 学习了,后期可能用到
楼主大能,感谢感谢
页:
[1]