用api获取系统的时间,可以精确到毫秒,用timer控件判断时间的改变。
Private Const CB_SETCURSEL = &H14E
Private Type uSystemTime
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
'Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal CX As Long, ByVal CY As Long, ByVal wFlags As Long) As Long
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As uSystemTime)
'
'名称 Fun_GetLocalTime
'作者 MornWoo20071016
'功能 获取系统的时间,精确到毫秒。
'参数 无
'返回 返回字符串数据: iFormat = 0,"YYYY-MM-DD HH:MM:SS"+" " + "毫秒数";iFormat=1,yyyymmddhhmmss毫秒数
' iFormat = 2,"YYYY-MM-DD HH:MM:SS"
'
Function Fun_GetLocalTime(Optional ByVal iFormat = 0) As String
Dim uSysTime As uSystemTime
Dim sA
GetSystemTime uSysTime
If CInt(uSysTimewHour + 8) < 24 Then
Select Case iFormat
Case 0
sA = Format(CStr(uSysTimewYear) & "-" & CStr(uSysTimewMonth) & "-" & CStr(uSysTimewDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTimewHour + 8), "00") & ":" & Format(CStr(uSysTimewMinute), "00") & ":" & _
Format(CStr(uSysTimewSecond), "00") & "" & CStr(uSysTimewMilliseconds)
Case 1
sA = Format(CStr(uSysTimewYear) & "-" & CStr(uSysTimewMonth) & "-" & CStr(uSysTimewDay), "yyyymmdd") & _
Format(CStr(uSysTimewHour + 8), "00") & Format(CStr(uSysTimewMinute), "00") & _
Format(CStr(uSysTimewSecond), "00") & CStr(uSysTimewMilliseconds)
Case 2
sA = Format(CStr(uSysTimewYear) & "-" & CStr(uSysTimewMonth) & "-" & CStr(uSysTimewDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTimewHour + 8), "00") & ":" & Format(CStr(uSysTimewMinute), "00") & ":" & _
Format(CStr(uSysTimewSecond), "00")
End Select
Else
Select Case iFormat
Case 0
sA = Format(CStr(uSysTimewYear) & "-" & CStr(uSysTimewMonth) & "-" & CStr(uSysTimewDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTimewMinute), "00") & _
":" & Format(CStr(uSysTimewSecond), "00") & "" & CStr(uSysTimewMilliseconds)
Case 1
sA = Format(CStr(uSysTimewYear) & "-" & CStr(uSysTimewMonth) & "-" & CStr(uSysTimewDay), "yyyymmdd") & _
Format(CStr(uSysTimewMinute), "00") & _
Format(CStr(uSysTimewSecond), "00") & CStr(uSysTimewMilliseconds)
Case 2
sA = Format(CStr(uSysTimewYear) & "-" & CStr(uSysTimewMonth) & "-" & CStr(uSysTimewDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTimewMinute), "00") & _
":" & Format(CStr(uSysTimewSecond), "00")
End Select
End If
Fun_GetLocalTime = sA
End Function
倒 用api函数
GetTickCount
VB声明
Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
说明
用于获取自windows启动以来经历的时间长度(毫秒)
返回值
Long,以毫秒为单位的windows运行时间
在开头和结束程序时 分别获取时间 相减就行啦
那函数还能获取开机时间哦
如果要精确到毫秒,估计只能使用api了,因为在执行的时候,就算只是执行加1 *** 作,他也要一定的时间,就算小于一毫秒,当它累积之后,误差就会变大。出来的方法,最好就是使在定时器里执行的代码尽量简单,以减小误差。
vb中获取系统当前的时间用函数 now() 就可以。另外还有相关的时间 date() time()
now()获取系统当前日期和时间,如:2015-12-12 22:23:34
time()获取系统的时间,如:22:23:34不显示当前日期
date()获得系统的日期,如:2015-12-12
Private Sub Command1_Click()
MsgBox Now()
MsgBox Time()
MsgBox Date
End Sub
代码修改如下:
Option Explicit
Private Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) '传回本地时间
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Dim SysTime As SYSTEMTIME
Private Sub Form_Load()
Text1Text = "00:00:00:00"
Command1Caption = "开始"
Command2Caption = "结束"
Command2Enabled = False
Timer1Interval = 1
Timer1Enabled = False
End Sub
Private Sub Command1_Click()
If Command1Caption = "开始" Then
Timer1Enabled = True
Command1Caption = "暂停"
Command2Enabled = True
ElseIf Command1Caption = "暂停" Then
Timer1Enabled = False
Command1Caption = "继续"
ElseIf Command1Caption = "继续" Then
Timer1Enabled = True
Command1Caption = "暂停"
End If
End Sub
Private Sub Command2_Click()
Timer1Enabled = False
Command2Enabled = False
Text1Text = "00:00:00:00"
Command1Caption = "开始"
Command1SetFocus
End Sub
Private Sub Timer1_Timer()
GetLocalTime SysTime
Text1Text = SysTimewHour & ":" & Format(SysTimewMinute, "0#") & ":" & Format(SysTimewSecond, "0#") & ":" & SysTimewMilliseconds
End Sub
直接用vb转换GMT时间
Private Function getWebDatetime() As String
Dim Xml>
Set Xml>
Xml>
Xml>
getWebDatetime = CDate(1 / 3 + CDbl(CDate(Mid$(Xml>
Set Xml>
End Function
扩展资料:
读取网站服务器返回的时间的代码
Private Function getWebDatetime() As String
Dim Xml>
Set Xml>
Xml>
Xml>
Set objJs = CreateObject("msscriptcontrolscriptcontrol")
objJsLanguage = "jScript"
getWebDatetime = objJsEval("var dt = new Date('" & Xml>
Set Xml>
Set objJs = Nothing
End Function
以上就是关于如何VB程序做一个软件,让其显示电脑上的日期和时间,秒钟是可以动得全部的内容,包括:如何VB程序做一个软件,让其显示电脑上的日期和时间,秒钟是可以动得、VB 如何知道一个程序执行完用了多少时间、怎么做vb计时器,精确到毫秒的,我把timer设成每毫秒+1,但不准,怎么办等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)