在窗體中建一個文本框和時間控件,可以在文本框中顯示跳動的服務器時間
為礦區(qū)等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及礦區(qū)網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為網(wǎng)站設計、成都網(wǎng)站設計、礦區(qū)網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
Private Sub Form_Load()
Timer1.Interval = 300
End Sub
Private Sub Timer1_Timer()
Dim cnn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rst As New ADODB.Recordset
Dim Sqlser_time
cnn.ConnectionString = "driver={SQL Server};" _
"server=server;uid=sa;pwd=;database=master"
cnn.Open
cmd.ActiveConnection = cnn
cmd.CommandText = "SELECT GETDATE() AS sys_Sqlser_time"
Set rst = cmd.Execute
Sqlser_time = rst(0)
Text1.Text = Sqlser_time
rst.Close
cnn.Close
End Sub
你是指Internet上的國際標準時間嗎。
先加入控件 AxWinsock1 在.Com中Microsoft.Winsock
Public Class Form1
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
Dim NoSrv As Boolean
Dim TimeFromNet As String
Dim OldTime As Date
Dim NewTime As Date
Dim MyDate As Date
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If AxWinsock1.CtlState MSWinsockLib.StateConstants.sckClosed Then AxWinsock1.Close()
AxWinsock1.Protocol = MSWinsockLib.ProtocolConstants.sckTCPProtocol
NetTime("") '首先取中科院國家授時中心時間
If NoSrv Or TimeFromNet = "" Then
NetTime("time.nist.gov") '取美國標準技時院時間
If NoSrv Or TimeFromNet = "" Then
MsgBox("檢測不到網(wǎng)絡標準時間服務器time.nist.gov!")
Else
NetTime("time.nist.gov")
If TimeFromNet = "" Then
MsgBox("網(wǎng)絡標準時間服務器time.nist.gov超時!")
Else
MyDate = Mid(TimeFromNet, 8, 8)
OldTime = Mid(TimeFromNet, 17, 8)
NewTime = TimeSerial((Hour(OldTime) + 8) Mod 24, Minute(OldTime), Second(OldTime))
Dim MyTime As DateTime = MyDate Space(1) NewTime
MsgBox(MyTime)
'SetWindowsClock(MyTime)
End If
End If
Else
'使網(wǎng)絡誤差時間小,第2次再中科院國家授時中心時間
NetTime("")
If TimeFromNet = "" Then
MsgBox("網(wǎng)絡標準時間服務器超時!")
Else
MyDate = Mid(TimeFromNet, 8, 8)
NewTime = Mid(TimeFromNet, 17, 8)
Dim MyTime As DateTime = MyDate Space(1) NewTime
MsgBox(MyTime)
'SetWindowsClock(MyTime)
End If
End If
End Sub
Private Sub NetTime(ByVal TimeSrv As String)
NoSrv = False
TimeFromNet = ""
If AxWinsock1.CtlState MSWinsockLib.StateConstants.sckClosed Then AxWinsock1.Close()
AxWinsock1.RemoteHost = TimeSrv ' "" 或 "time.nist.gov"
AxWinsock1.RemotePort = 13
AxWinsock1.LocalPort = 0
AxWinsock1.Connect()
Do While TimeFromNet = ""
If NoSrv Then Exit Do
Sleep(55)
System.Windows.Forms.Application.DoEvents()
Loop
If AxWinsock1.CtlState MSWinsockLib.StateConstants.sckClosed Then AxWinsock1.Close()
End Sub
Private Sub AxAxWinsock1_CloseEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxWinsock1.CloseEvent
AxWinsock1.Close()
End Sub
Private Sub AxAxWinsock1_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles AxWinsock1.DataArrival
AxWinsock1.GetData(TimeFromNet)
End Sub
Private Sub AxAxWinsock1_Error(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles AxWinsock1.Error
NoSrv = True
End Sub
End Class
最簡單的辦法是用Shell調用NET TIME //servername 命令獲得時間,你可以將輸出重定向到一個文件,然后在VB讀取這個文件以獲得時間。 ---------------------------------------或者服務器上有SQL服務的話,可以:Set DbC = New ADODB.Connection
Dim adoDateTime As New ADODB.Recordset '獲取 NT-SERVER 時間
With DbC
If .State = adStateOpen Then .Close
.CursorLocation = adUseClient
.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" Server ";UID=" uID ";PWD=" uPassword ";DATABASE=" MyDatabase ";OPTION=1 + 2 + 8 + 32 + 2048 + 163841"
.ConnectionTimeout = 90
.Open
adoDateTime.Open "select now() as mysqlTime"
End With ————————————————————————————————————————————使用Windows API的NetRemoteTOD函數(shù)獲得服務器的時間Option Explicit
Private Declare Function NetRemoteTOD Lib "Netapi32.dll" ( _
tServer As Any, pBuffer As Long) As Long
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
Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(32) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(32) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type
Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
Private Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As Long) As Long
'
Private Type TIME_OF_DAY_INFO
tod_elapsedt As Long
tod_msecs As Long
tod_hours As Long
tod_mins As Long
tod_secs As Long
tod_hunds As Long
tod_timezone As Long
tod_tinterval As Long
tod_day As Long
tod_month As Long
tod_year As Long
tod_weekday As Long
End Type
'
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function getRemoteTOD(ByVal strServer As String) As Date
Dim result As Date
Dim lRet As Long
Dim tod As TIME_OF_DAY_INFO
Dim lpbuff As Long
Dim tServer() As Byte
tServer = strServer vbNullChar
lRet = NetRemoteTOD(tServer(0), lpbuff)
If lRet = 0 Then
CopyMemory tod, ByVal lpbuff, Len(tod)
NetApiBufferFree lpbuff
result = DateSerial(tod.tod_year, tod.tod_month, tod.tod_day) + _
TimeSerial(tod.tod_hours, tod.tod_mins - tod.tod_timezone, tod.tod_secs)
getRemoteTOD = result
Else
Err.Raise Number:=vbObjectError + 1001, _
Description:="cannot get remote TOD"
End If
End Function
'要運行該程序,通過如下方式調用。
Private Sub Command1_Click()
Dim d As Date
d = getRemoteTOD("\\trademark")
MsgBox d
End Sub