Public Function GetServerDate() As Date '***************************************** ' '获取服务器时间的函数 '***************************************** On Error GoTo errhandle Dim Cnn As ADODB.Connection Dim cmd As ADODB.Command Dim para1 As ADODB.Parameter Set Cnn = New ADODB.Connection Set para1 = New ADODB.Parameter Set cmd = New ADODB.Command Cnn.ConnectionString = ConnectString Cnn.Open cmd.CommandText = "my_get_severtime" cmd.ActiveConnection = Cnn cmd.CommandType = adCmdStoredProc Set para1 = cmd.CreateParameter("mydate", adDate, adParamOutput) cmd.Parameters.Append para1 cmd.Execute GetServerDate = cmd.Parameters("mydate") Set Cnn = Nothing Set cmd = Nothing Set para1 = Nothing Exit Function errhandle: '如果获得服务器时间出错,则调用本地时间 GetServerDate = Date Set Cnn = Nothing Set cmd = Nothing Set para1 = Nothing End Function Public Function GetServerTime() As Date '*************************************** '取服务器时间 '*************************************** Dim Sqlstring As String Dim Rst As ADODB.Recordset Set Rst = New ADODB.Recordset Sqlstring = "select substring(convert(varchar(20),getdate(),20),12,9)" Set Rst = ExecuteSQL(Sqlstring) On Error GoTo errhandle GetServerTime = CDate(Rst.Fields(0).Value) Rst.Close Set Rst = Nothing errhandle: '出错取本地时间 GetServerTime = Time Set Cnn = Nothing End Function
我上面的用了存储过程的.... 其实直接Select Getdate()就可以了.
如果是SQL服務器,用SELECT Getdate()就行了, 非數據庫服務器可用如下代碼: Option Explicit'API Structures Private Type TIME_OF_DAY_INFO tod_elapsed 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'NetAPI Calls Private Declare Function NetRemoteTOD Lib "netapi32.dll" (yServer As Any, pBuffer As Long) As Long Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal pBuffer As Long) As Long 'Kernel API Calls Private Declare Sub CopyMem Lib "kernel32.dll" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
'Return the Time and Date of a specified Machine on the Net Public Function GetRemoteTime(ServerName As String) As Date Dim lpBuffer As Long Dim t_struct As TIME_OF_DAY_INFO Dim ret As Long Dim bServer() As Byte If Trim(ServerName) = "" Then 'Local machine ret = NetRemoteTOD(vbNullString, lpBuffer) Else 'Check the syntax of the ServerName string If InStr(ServerName, "\\") = 1 Then bServer = ServerName & vbNullChar Else bServer = "\\" & ServerName & vbNullChar End If ret = NetRemoteTOD(bServer(0), lpBuffer) End If CopyMem t_struct, ByVal lpBuffer, Len(t_struct) If lpBuffer Then Call NetApiBufferFree(lpBuffer) End If GetRemoteTime = DateSerial(t_struct.tod_year, t_struct.tod_month, t_struct.tod_day) + TimeSerial(t_struct.tod_hours, t_struct.tod_mins - t_struct.tod_timezone, t_struct.tod_secs) End Function'Get the time and date of the local machine Private Sub Command1_Click() MsgBox GetRemoteTime("") End Sub'Get the time and date a remote Workstation Private Sub Command2_Click() MsgBox GetRemoteTime("BE_mail") End Sub
数据库服务器可以通过Sql语句得到,如select getdate()
如是系统服务器,则个人认为最好通过“服务”来实现
SQL里getdate()
至于服务的实现,建议看有关”创建windows服务方面的资料“
'*****************************************
' '获取服务器时间的函数
'*****************************************
On Error GoTo errhandle
Dim Cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim para1 As ADODB.Parameter
Set Cnn = New ADODB.Connection
Set para1 = New ADODB.Parameter
Set cmd = New ADODB.Command
Cnn.ConnectionString = ConnectString
Cnn.Open
cmd.CommandText = "my_get_severtime"
cmd.ActiveConnection = Cnn
cmd.CommandType = adCmdStoredProc
Set para1 = cmd.CreateParameter("mydate", adDate, adParamOutput)
cmd.Parameters.Append para1
cmd.Execute
GetServerDate = cmd.Parameters("mydate")
Set Cnn = Nothing
Set cmd = Nothing
Set para1 = Nothing
Exit Function
errhandle: '如果获得服务器时间出错,则调用本地时间
GetServerDate = Date
Set Cnn = Nothing
Set cmd = Nothing
Set para1 = Nothing
End Function
Public Function GetServerTime() As Date
'***************************************
'取服务器时间
'***************************************
Dim Sqlstring As String
Dim Rst As ADODB.Recordset
Set Rst = New ADODB.Recordset
Sqlstring = "select substring(convert(varchar(20),getdate(),20),12,9)"
Set Rst = ExecuteSQL(Sqlstring)
On Error GoTo errhandle
GetServerTime = CDate(Rst.Fields(0).Value)
Rst.Close
Set Rst = Nothing
errhandle: '出错取本地时间
GetServerTime = Time
Set Cnn = Nothing
End Function
其实直接Select Getdate()就可以了.
非數據庫服務器可用如下代碼:
Option Explicit'API Structures
Private Type TIME_OF_DAY_INFO
tod_elapsed 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'NetAPI Calls
Private Declare Function NetRemoteTOD Lib "netapi32.dll" (yServer As Any, pBuffer As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal pBuffer As Long) As Long
'Kernel API Calls
Private Declare Sub CopyMem Lib "kernel32.dll" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
'Return the Time and Date of a specified Machine on the Net
Public Function GetRemoteTime(ServerName As String) As Date
Dim lpBuffer As Long
Dim t_struct As TIME_OF_DAY_INFO
Dim ret As Long
Dim bServer() As Byte If Trim(ServerName) = "" Then
'Local machine
ret = NetRemoteTOD(vbNullString, lpBuffer)
Else
'Check the syntax of the ServerName string
If InStr(ServerName, "\\") = 1 Then
bServer = ServerName & vbNullChar
Else
bServer = "\\" & ServerName & vbNullChar
End If
ret = NetRemoteTOD(bServer(0), lpBuffer)
End If
CopyMem t_struct, ByVal lpBuffer, Len(t_struct)
If lpBuffer Then
Call NetApiBufferFree(lpBuffer)
End If
GetRemoteTime = DateSerial(t_struct.tod_year, t_struct.tod_month, t_struct.tod_day) + TimeSerial(t_struct.tod_hours, t_struct.tod_mins - t_struct.tod_timezone, t_struct.tod_secs)
End Function'Get the time and date of the local machine
Private Sub Command1_Click()
MsgBox GetRemoteTime("")
End Sub'Get the time and date a remote Workstation
Private Sub Command2_Click()
MsgBox GetRemoteTime("BE_mail")
End Sub
SQL里getdate()