Option Explicit'Task: Tells you how long you have been in Windows using the GetTickCount and GetSystemTime API.'DeclarationsPrivate Type SYSTEMTIME ' 16 Bytes 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 TypePrivate Declare Function GetTickCount Lib "kernel32" () As Long Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)Private Sub Command1_Click() Dim tmp, tmpTime, tmpHours, tmpMinutes, tmpSeconds, tmpMilliSeconds As String Dim nYear, nMonth, nDayOfWeek, nDay, nHour, nMinute, nSecond, nMilliseconds As String Dim NowTime As SYSTEMTIME 'Time is given in Milliseconds tmpTime = Format(GetTickCount, "0") 'Converts Milliseconds to Hours,Minutes,Seconds and Milliseconds 'There is most probably a better way to do this tmpMilliSeconds = Right$(tmpTime, 3) tmpTime = Format(Left$(tmpTime, Len(tmpTime) - 3) / 60, "0.0000000000") tmpHours = Format(tmpTime / 60, "0") tmpMinutes = Format(Left$(tmpTime, Len(tmpTime) - 11) - (tmpHours * 60), "00") tmpSeconds = Format(Round(("0." & Right$(tmpTime, 10)) * 60), "00") GetSystemTime NowTime nYear = Format(NowTime.wYear) nMonth = Format(NowTime.wMonth) nDay = Format(NowTime.wDay) nHour = Format(NowTime.wHour + 8) nMinute = Format(NowTime.wMinute) nSecond = Format(NowTime.wSecond) nMilliseconds = Format(NowTime.wMilliseconds) If NowTime.wDayOfWeek = 0 Then nDayOfWeek = "Sunday" ElseIf NowTime.wDayOfWeek = 1 Then nDayOfWeek = "Monday" ElseIf NowTime.wDayOfWeek = 2 Then nDayOfWeek = "Tuesday" ElseIf NowTime.wDayOfWeek = 3 Then nDayOfWeek = "Wednesday" ElseIf NowTime.wDayOfWeek = 4 Then nDayOfWeek = "Thursday" ElseIf NowTime.wDayOfWeek = 5 Then nDayOfWeek = "Friday" ElseIf NowTime.wDayOfWeek = 6 Then nDayOfWeek = "Saturday" End If 'Display the time lblTime = "Windows has run " & tmpHours & " : " & tmpMinutes & " : " & tmpSeconds & " : " & tmpMilliSeconds LabelNow.Caption = nYear & "-" & nMonth & "-" & nDay & " " & nHour & ":" & nMinute & ":" & nSecond & " " & nDayOfWeek End Sub
利用Api函数计算Windows从启动后所运行的总时间 Private Declare Function GetTickCount Lib "kernel32" () As LongPrivate Sub Timer1_Timer() Dim hour As Integer Dim minute As Integer Dim second As Integer hour = GetTickCount \ 1000 \ 60 \ 60 Label1.Caption = Str(hour) + "小时"minute = (GetTickCount - hour * 60 * 60 * 1000) \ 1000 \ 60 Label2.Caption = Str(minute) + "分钟"second = (GetTickCount - Val(Label1.Caption) * 60 * 60 * 1000 - Val(Label2.Caption) * 60 * 1000) \ 1000 Label3.Caption = Str(second) + "秒钟" End Sub
GetTickCount VB声明 Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long 说明 用于获取自windows启动以来经历的时间长度(毫秒) 返回值 Long,以毫秒为单位的windows运行时间
GetSystemTime VB声明 Declare Sub GetSystemTime Lib "kernel32" Alias "GetSystemTime" (lpSystemTime As SYSTEMTIME) 说明 在一个SYSTEMTIME中载入当前系统时间,这个时间采用的是“协同世界时间”(即UTC,也叫做GMT)格式 参数表 参数 类型及说明 lpSystemTime SYSTEMTIME,随同当前时间载入的结构 =====你用当前系统时间减去开机总的时间即为开机时间
Date 函数 返回当前系统日期。 Day 函数 返回 1 到 31 之间的一个整数(包括 1 和31),代表某月中的一天。 语法 Day(date) date 参数是任意可以代表日期的表达式。如果 date 参数中包含 Null,则返回 Null。 Hour 函数描述 返回 0 到 23 之间的一个整数(包括 0 和 23),代表一天中的某一小时。 语法 Hour(time) time 参数是任意可以代表时间的表达式。如果 time 参数中包含 Null,则返回 Null。Minute 函数描述 返回 0 到 59 之间的一个整数(包括 0 和59),代表一小时内的某一分钟。 语法 Minute(time) time 参数是任意可以代表时间的表达式。如果 time 参数包含 Null,则返回 Null。Year 函数描述 返回一个代表某年的整数。 语法 Year(date) date 参数是任意可以代表日期的参数。如果 date 参数中包含 Null,则返回 Null。Month 函数描述 返回 1 到 12 之间的一个整数(包括 1 和 12),代表一年中的某月。 语法 Month(date) date 参数是任意可以代表日期的表达式。如果 date 参数中包含 Null,则返回 Null。 Second 函数描述 返回 0 到 59 之间的一个整数(包括 1 和 59),代表一分钟内的某一秒。 语法 Second(time) time 参数是任意可以代表时间的表达式。如果 time 参数中包含 Null,则返回 Null。Time 函数描述 返回 Date 子类型 Variant,指示当前系统时间。 语法 Time Weekday 函数描述 返回代表一星期中某天的整数。 语法 Weekday(date, [firstdayofweek]) Weekday 函数的语法有以下参数:参数 描述 date 可以代表日期的任意表达式。如果 date 参数中包含 Null,则返回 Null。 firstdayofweek 指定星期中第一天的常数。如果省略,默认使用 vbSunday。 设置 firstdayofweek 参数有如下设置: 常数 值 描述 vbUseSystem 0 使用区域语言支持 (NLS) API 设置。 vbSunday 1 星期日 vbMonday 2 星期一 vbTuesday 3 星期二 vbWednesday 4 星期三 vbThursday 5 星期四 vbFriday 6 星期五 vbSaturday 7 星期六 返回值 Weekday 函数返回如下值: 常数 值 描述 vbSunday 1 星期日 vbMonday 2 星期一 vbTuesday 3 星期二 vbWednesday 4 星期三 vbThursday 5 星期四 vbFriday 6 星期五 vbSaturday 7 星期六 例子 Dim sT As String
sT = "现在时间是:" & Now & vbTab & "现在日期是:" & Date & vbTab & "现在时刻是:" & Time & vbCrLf sT = sT & "年:" & Year(Date) & vbTab & "月:" & Month(Date) & vbTab & "日:" & Day(Date) & vbCrLf sT = sT & "时:" & Hour(Time) & vbTab & "分:" & Minute(Time) & vbTab & "秒:" & Second(Time)
MsgBox sT
Private Declare Function GetTickCount Lib "kernel32" () As Long Private Sub Form_Load()End SubPrivate Sub Label1_DblClick() EndEnd SubPrivate Sub Timer1_Timer() '系统工作时间 Call GetTickCount h = Int(GetTickCount / 1000 / 3600) m = Int((GetTickCount / 1000 / 3600 - h) * 60) Label1.Caption = "系统已经工作" & h & "小时" & m & "分钟"
End Sub
我这个用到API函数的比较简单
Private Declare Function GetTickCount Lib "kernel32" () As Long Private Sub Timer1_Timer() '系统工作时间 Call GetTickCount h = Int(GetTickCount / 1000 / 3600) m = Int((GetTickCount / 1000 / 3600 - h) * 60) Label1.Caption = "系统已经工作" & h & "小时" & m & "分钟" End Sub
用TIME()函数得到开始运行的时候和结束的时间,两者一减就是总运行时间.
利用Api函数计算Windows从启动后所运行的总时间
Windows 运行了多长时间 声明: Declare Function GetTickCount& Lib "kernel32" () 使用该函数,可以得到从开机开时的运行时间,以 1/1000 秒记数。
If the function succeeds, the return value is the number of milliseconds that have elapsed since Windows was started.
Option Explicit'Task: Tells you how long you have been in Windows using the GetTickCount and GetSystemTime API.'DeclarationsPrivate Type SYSTEMTIME ' 16 Bytes
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 TypePrivate Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)Private Sub Command1_Click()
Dim tmp, tmpTime, tmpHours, tmpMinutes, tmpSeconds, tmpMilliSeconds As String
Dim nYear, nMonth, nDayOfWeek, nDay, nHour, nMinute, nSecond, nMilliseconds As String
Dim NowTime As SYSTEMTIME
'Time is given in Milliseconds
tmpTime = Format(GetTickCount, "0")
'Converts Milliseconds to Hours,Minutes,Seconds and Milliseconds
'There is most probably a better way to do this
tmpMilliSeconds = Right$(tmpTime, 3)
tmpTime = Format(Left$(tmpTime, Len(tmpTime) - 3) / 60, "0.0000000000")
tmpHours = Format(tmpTime / 60, "0")
tmpMinutes = Format(Left$(tmpTime, Len(tmpTime) - 11) - (tmpHours * 60), "00")
tmpSeconds = Format(Round(("0." & Right$(tmpTime, 10)) * 60), "00")
GetSystemTime NowTime
nYear = Format(NowTime.wYear)
nMonth = Format(NowTime.wMonth)
nDay = Format(NowTime.wDay)
nHour = Format(NowTime.wHour + 8)
nMinute = Format(NowTime.wMinute)
nSecond = Format(NowTime.wSecond)
nMilliseconds = Format(NowTime.wMilliseconds)
If NowTime.wDayOfWeek = 0 Then
nDayOfWeek = "Sunday"
ElseIf NowTime.wDayOfWeek = 1 Then
nDayOfWeek = "Monday"
ElseIf NowTime.wDayOfWeek = 2 Then
nDayOfWeek = "Tuesday"
ElseIf NowTime.wDayOfWeek = 3 Then
nDayOfWeek = "Wednesday"
ElseIf NowTime.wDayOfWeek = 4 Then
nDayOfWeek = "Thursday"
ElseIf NowTime.wDayOfWeek = 5 Then
nDayOfWeek = "Friday"
ElseIf NowTime.wDayOfWeek = 6 Then
nDayOfWeek = "Saturday"
End If
'Display the time
lblTime = "Windows has run " & tmpHours & " : " & tmpMinutes & " : " & tmpSeconds & " : " & tmpMilliSeconds
LabelNow.Caption = nYear & "-" & nMonth & "-" & nDay & " " & nHour & ":" & nMinute & ":" & nSecond & " " & nDayOfWeek
End Sub
Dim hour As Integer
Dim minute As Integer
Dim second As Integer
hour = GetTickCount \ 1000 \ 60 \ 60
Label1.Caption = Str(hour) + "小时"minute = (GetTickCount - hour * 60 * 60 * 1000) \ 1000 \ 60
Label2.Caption = Str(minute) + "分钟"second = (GetTickCount - Val(Label1.Caption) * 60 * 60 * 1000 - Val(Label2.Caption) * 60 * 1000) \ 1000
Label3.Caption = Str(second) + "秒钟"
End Sub
Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
说明
用于获取自windows启动以来经历的时间长度(毫秒)
返回值
Long,以毫秒为单位的windows运行时间
Declare Sub GetSystemTime Lib "kernel32" Alias "GetSystemTime" (lpSystemTime As SYSTEMTIME)
说明
在一个SYSTEMTIME中载入当前系统时间,这个时间采用的是“协同世界时间”(即UTC,也叫做GMT)格式
参数表
参数 类型及说明
lpSystemTime SYSTEMTIME,随同当前时间载入的结构 =====你用当前系统时间减去开机总的时间即为开机时间
Day 函数 返回 1 到 31 之间的一个整数(包括 1 和31),代表某月中的一天。
语法
Day(date)
date 参数是任意可以代表日期的表达式。如果 date 参数中包含 Null,则返回 Null。
Hour 函数描述
返回 0 到 23 之间的一个整数(包括 0 和 23),代表一天中的某一小时。
语法
Hour(time)
time 参数是任意可以代表时间的表达式。如果 time 参数中包含 Null,则返回 Null。Minute 函数描述
返回 0 到 59 之间的一个整数(包括 0 和59),代表一小时内的某一分钟。
语法
Minute(time)
time 参数是任意可以代表时间的表达式。如果 time 参数包含 Null,则返回 Null。Year 函数描述
返回一个代表某年的整数。
语法
Year(date)
date 参数是任意可以代表日期的参数。如果 date 参数中包含 Null,则返回 Null。Month 函数描述
返回 1 到 12 之间的一个整数(包括 1 和 12),代表一年中的某月。
语法
Month(date)
date 参数是任意可以代表日期的表达式。如果 date 参数中包含 Null,则返回 Null。
Second 函数描述
返回 0 到 59 之间的一个整数(包括 1 和 59),代表一分钟内的某一秒。
语法
Second(time)
time 参数是任意可以代表时间的表达式。如果 time 参数中包含 Null,则返回 Null。Time 函数描述
返回 Date 子类型 Variant,指示当前系统时间。
语法
Time
Weekday 函数描述
返回代表一星期中某天的整数。
语法
Weekday(date, [firstdayofweek])
Weekday 函数的语法有以下参数:参数 描述
date 可以代表日期的任意表达式。如果 date 参数中包含 Null,则返回 Null。
firstdayofweek 指定星期中第一天的常数。如果省略,默认使用 vbSunday。
设置
firstdayofweek 参数有如下设置:
常数 值 描述
vbUseSystem 0 使用区域语言支持 (NLS) API 设置。
vbSunday 1 星期日
vbMonday 2 星期一
vbTuesday 3 星期二
vbWednesday 4 星期三
vbThursday 5 星期四
vbFriday 6 星期五
vbSaturday 7 星期六
返回值
Weekday 函数返回如下值:
常数 值 描述
vbSunday 1 星期日
vbMonday 2 星期一
vbTuesday 3 星期二
vbWednesday 4 星期三
vbThursday 5 星期四
vbFriday 6 星期五
vbSaturday 7 星期六 例子
Dim sT As String
sT = "现在时间是:" & Now & vbTab & "现在日期是:" & Date & vbTab & "现在时刻是:" & Time & vbCrLf
sT = sT & "年:" & Year(Date) & vbTab & "月:" & Month(Date) & vbTab & "日:" & Day(Date) & vbCrLf
sT = sT & "时:" & Hour(Time) & vbTab & "分:" & Minute(Time) & vbTab & "秒:" & Second(Time)
MsgBox sT
Private Sub Form_Load()End SubPrivate Sub Label1_DblClick()
EndEnd SubPrivate Sub Timer1_Timer()
'系统工作时间
Call GetTickCount
h = Int(GetTickCount / 1000 / 3600)
m = Int((GetTickCount / 1000 / 3600 - h) * 60)
Label1.Caption = "系统已经工作" & h & "小时" & m & "分钟"
End Sub
Private Sub Timer1_Timer()
'系统工作时间
Call GetTickCount
h = Int(GetTickCount / 1000 / 3600)
m = Int((GetTickCount / 1000 / 3600 - h) * 60)
Label1.Caption = "系统已经工作" & h & "小时" & m & "分钟"
End Sub
声明:
Declare Function GetTickCount& Lib "kernel32" () 使用该函数,可以得到从开机开时的运行时间,以 1/1000 秒记数。