找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 4282|回复: 4

【VB】“HH:MM:SS”格式的时间的计算

[复制链接]
发表于 2014-7-6 22:51:55 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
原理很简单。转换成秒。然后进行计算(加减乘除与或非、左移右移等)。最后再转换回HH:MM:SS的格式。
看这界面!多直观!还支持16进制!
20140706224338.png
注意我用的是VB6哈。不是.NET
BIN下载: 时间计算.exe (24 KB, 下载次数: 0, 售价: 1 个宅币)
SRC下载: 时间计算.7z (6.46 KB, 下载次数: 5, 售价: 10 个宅币)
顺带我把源代码放出来。一览无余。
  1. VERSION 5.00
  2. Begin VB.Form frmTimeCalc
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "时间计算器"
  5.    ClientHeight    =   495
  6.    ClientLeft      =   45
  7.    ClientTop       =   375
  8.    ClientWidth     =   6495
  9.    Icon            =   "frmTimeCalc.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    ScaleHeight     =   33
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   433
  15.    StartUpPosition =   3  '窗口缺省
  16.    Begin VB.ComboBox CbOperator
  17.       Height          =   300
  18.       ItemData        =   "frmTimeCalc.frx":000C
  19.       Left            =   2040
  20.       List            =   "frmTimeCalc.frx":002B
  21.       Style           =   2  'Dropdown List
  22.       TabIndex        =   4
  23.       Top             =   120
  24.       Width           =   855
  25.    End
  26.    Begin VB.TextBox txtResult
  27.       BackColor       =   &H8000000F&
  28.       Height          =   270
  29.       Left            =   5400
  30.       Locked          =   -1  'True
  31.       TabIndex        =   3
  32.       Text            =   "00:00:00"
  33.       Top             =   120
  34.       Width           =   975
  35.    End
  36.    Begin VB.CommandButton Calc
  37.       Caption         =   "="
  38.       Height          =   255
  39.       Left            =   4920
  40.       TabIndex        =   2
  41.       Top             =   120
  42.       Width           =   375
  43.    End
  44.    Begin VB.TextBox txtTime2
  45.       Height          =   270
  46.       Left            =   3000
  47.       TabIndex        =   1
  48.       Text            =   "00:00:00"
  49.       Top             =   120
  50.       Width           =   1815
  51.    End
  52.    Begin VB.TextBox txtTime1
  53.       Height          =   270
  54.       Left            =   120
  55.       TabIndex        =   0
  56.       Text            =   "00:00:00"
  57.       Top             =   120
  58.       Width           =   1815
  59.    End
  60. End
  61. Attribute VB_Name = "frmTimeCalc"
  62. Attribute VB_GlobalNameSpace = False
  63. Attribute VB_Creatable = False
  64. Attribute VB_PredeclaredId = True
  65. Attribute VB_Exposed = False
  66. '作者:0xAA55
  67. '论坛:[url]http://www.0xaa55.com/[/url]
  68. '版权所有(C) 2013-2014 技术宅的结界
  69. '请保留原作者信息否则视为侵权
  70. Option Explicit

  71. '为了保证我们不在地球的时候也能用上这个程序,我把这些常数弄了一下
  72. Private Const SecondsPerMinute As Long = 60 '每分钟秒数
  73. Private Const MinutesPerHour As Long = 60 '每小时分钟数
  74. Private Const HoursPerDay As Long = 24 '每天的小时数
  75. Private Const SecondsPerHour As Long = SecondsPerMinute * MinutesPerHour '每小时的秒数
  76. Private Const SecondsPerDay As Long = SecondsPerHour * HoursPerDay '每天的秒数

  77. 'TimeToSeconds:
  78. '将描述时间的字符串转换成以秒计数的数值。字符串的格式是 小时:分钟:秒
  79. Function TimeToSeconds(ByVal TimeString As String) As Long
  80. On Error GoTo ErrHandler
  81. Dim Hours&, Minutes&, Seconds&, Strings$()
  82. Strings = Split(TimeString, ":")
  83. Hours = CLng(Replace(Strings(0), "0x", "&H"))
  84. Minutes = CLng(Replace(Strings(1), "0x", "&H"))
  85. Seconds = CLng(Replace(Strings(2), "0x", "&H"))
  86. TimeToSeconds = Seconds + Minutes * SecondsPerMinute + Hours * SecondsPerHour
  87. Exit Function
  88. ErrHandler:
  89. TimeToSeconds = -1
  90. End Function

  91. 'SecondsToTime:
  92. '将描述时间的以秒计数的数值转换成字符串。字符串的格式是 小时:分钟:秒
  93. Function SecondsToTime(ByVal TimeSeconds As Long) As String
  94. Dim Hours&, Minutes&, Seconds&, Sign As String
  95. If TimeSeconds < 0 Then
  96.     TimeSeconds = -TimeSeconds
  97.     Sign = "-"
  98. End If
  99. Seconds = TimeSeconds Mod SecondsPerMinute
  100. Minutes = (TimeSeconds \ SecondsPerMinute) Mod MinutesPerHour
  101. Hours = (TimeSeconds \ SecondsPerHour) Mod HoursPerDay
  102. SecondsToTime = Sign & Format$(Hours, "00") & ":" & Format$(Minutes, "00") & ":" & Format$(Seconds, "00")
  103. End Function

  104. Private Sub Calc_Click()
  105. On Error Resume Next

  106. '先全部转换成秒
  107. Dim Seconds1 As Long, Seconds2 As Long
  108. Seconds1 = TimeToSeconds(txtTime1.Text)
  109. Seconds2 = TimeToSeconds(txtTime2.Text)
  110. If Seconds1 = -1 Or Seconds2 = -1 Then
  111.     MsgBox "时间格式不对。请按照“时:分:秒”的方式输入。", vbCritical, "无法计算。"
  112.     Exit Sub
  113. End If

  114. Dim ResultSeconds As Long

  115. '然后进行计算。支持⑨种计算
  116. Select Case CbOperator.ListIndex
  117.     Case 0 '加
  118.         ResultSeconds = (Seconds1 + Seconds2) Mod SecondsPerDay
  119.     Case 1 '减
  120.         ResultSeconds = (Seconds1 - Seconds2) Mod SecondsPerDay
  121.     Case 2 '乘
  122.         ResultSeconds = (Seconds1 * Seconds2) Mod SecondsPerDay
  123.     Case 3 '除
  124.         ResultSeconds = (Seconds1 \ Seconds2) Mod SecondsPerDay
  125.     Case 4 '与
  126.         ResultSeconds = (Seconds1 And Seconds2) Mod SecondsPerDay
  127.     Case 5 '或
  128.         ResultSeconds = (Seconds1 Or Seconds2) Mod SecondsPerDay
  129.     Case 6 '异或
  130.         ResultSeconds = (Seconds1 Xor Seconds2) Mod SecondsPerDay
  131.     Case 7 '左移
  132.         ResultSeconds = (Seconds1 * (2 ^ Seconds2)) Mod SecondsPerDay
  133.     Case 8 '右移
  134.         ResultSeconds = (Seconds1 \ (2 ^ Seconds2)) Mod SecondsPerDay
  135. End Select

  136. txtResult.Text = SecondsToTime(ResultSeconds)
  137. End Sub

  138. Private Sub Form_Load()
  139. CbOperator.ListIndex = 0
  140. End Sub
复制代码

本帖被以下淘专辑推荐:

回复

使用道具 举报

KxIX 该用户已被删除
发表于 2014-7-6 23:47:47 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 赞! 靠!

使用道具 举报

发表于 2014-12-21 07:13:47 | 显示全部楼层
vb学习中,非常有收获
回复 赞! 靠!

使用道具 举报

发表于 2021-9-1 14:16:17 | 显示全部楼层
学习了,后期可能用到
回复 赞! 靠!

使用道具 举报

发表于 2022-5-10 11:45:53 | 显示全部楼层

楼主大能,感谢感谢
回复 赞! 靠!

使用道具 举报

本版积分规则

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-9-8 09:40 , Processed in 0.042936 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表