Dim sLocalTime As String Dim aDateTime As Variant Dim aMonthDay As Variant Dim aYearTime As Variant Dim sDayName As String Dim sMonthDay As String Dim sYearTime As String Dim sMonth As String Dim sDay As String Dim sYear As String Dim sTime As StringPrivate Sub Cmd_Connect_Click() Winsock1.Connect StatusBar1.SimpleText = Winsock1.State End SubPrivate Sub Cmd_SetTime_Click() Cmd_Connect_Click 'refresh the time aDateTime = Split(Trim(Txt_DateTime.Text), ",") ' sDayName = aDateTime(0) sMonthDay = aDateTime(1) sYearTime = aDateTime(2) ' aMonthDay = Split(Trim(sMonthDay), " ") sMonth = aMonthDay(0) sDay = aMonthDay(1) ' aYearTime = Split(Trim(sYearTime), " ") sYear = aYearTime(0) sTime = aYearTime(1) sTime = Left(sTime, InStr(1, sTime, "-") - 1) 'remove -EST
' set the system date using day/month/year Date = sDay & "/" & sMonth & "/" & sYear Time = sTime End SubPrivate Sub Form_Load() Winsock1.RemoteHost = "unimelb.edu.au" 'use the address or IP for your server Winsock1.RemotePort = 13 End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Cmd_SetTime.Enabled = True Winsock1.GetData sLocalTime Txt_DateTime.Text = Trim(sLocalTime) Winsock1.Close End Sub
谢谢,其实这两个帖子我是同时发的,我只是想知道API和用sql语句哪个更好而已
我找到的一个用API求服务器时间的例子,可以看一下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 ti As TIME_OF_DAY_INFO Private Type HiLoInt loInt As Integer hiInt As Integer End TypeDim TwoInt As HiLoIntPrivate Type LongType l As Long End TypeDim lLong As LongTypePrivate Declare Function NetRemoteTOD Lib "netapi32.dll" (ServerName As Any, buffer As Any) As LongPrivate Declare Function DataFromPtr Lib "kernel32" Alias "lstrcpynW" (RetVal As Any, ByVal Ptr As Long, ByVal nCharCount As Long) As LongPrivate Declare Function NetAPIBufferFree Lib "netapi32.dll" Alias "NetApiBufferFree" (ByVal Ptr As Long) As Long Private Sub Command1_Click() Dim sName As String Dim pbServer() As Byte Dim ptmpBuffer As Long Dim lRetVal As Long ''Set the NT server name or IP sName = "\\zhengnanzn" ''Convert the name to a Unicode byte array pbServer = sName & vbNullChar ''Call the NetRemoteTOD function lRetVal = NetRemoteTOD(pbServer(0), ptmpBuffer) If lRetVal <> 0 Then ''Function failed Exit Sub End If ''Extract the information into a TIME_OF_DAY_INFO structure ''Get first element of the structure lRetVal = DataFromPtr(ti.tod_elapsedt, ptmpBuffer, 4) ''Get second element of the structure lRetVal = DataFromPtr(ti.tod_msecs, ptmpBuffer + 4, 4) ''Get third element of the structure lRetVal = DataFromPtr(ti.tod_hours, ptmpBuffer + 8, 4) ''.... lRetVal = DataFromPtr(ti.tod_mins, ptmpBuffer + 12, 4) lRetVal = DataFromPtr(ti.tod_secs, ptmpBuffer + 16, 4) lRetVal = DataFromPtr(ti.tod_hunds, ptmpBuffer + 20, 4) lRetVal = DataFromPtr(ti.tod_timezone, ptmpBuffer + 24, 4) lRetVal = DataFromPtr(ti.tod_tinterval, ptmpBuffer + 28, 4) lRetVal = DataFromPtr(ti.tod_day, ptmpBuffer + 32, 4) lRetVal = DataFromPtr(ti.tod_month, ptmpBuffer + 36, 4) lRetVal = DataFromPtr(ti.tod_year, ptmpBuffer + 40, 4) ''Get last element of the structure lRetVal = DataFromPtr(ti.tod_weekday, ptmpBuffer + 44, 4) ''Convert to local time If ti.tod_timezone <> -1 Then ti.tod_hours = ti.tod_hours - ti.tod_timezone \ 60 End If ''Release the memory, allocated by NetRemoteTOD function NetAPIBufferFree ptmpBuffer MsgBox "Time: " & ti.tod_hours & ":" & ti.tod_mins & ":" & ti.tod_secs MsgBox "Date: " & ti.tod_day & "/" & ti.tod_month & "/" & ti.tod_year End Sub
我已经回答过你的问题了现在又来问,不要搞的你问问题的,好象是来看戏的一样,让我们在这里瞎折腾http://expert.csdn.net/Expert/topic/1541/1541466.xml?temp=.3500177
Dim aDateTime As Variant
Dim aMonthDay As Variant
Dim aYearTime As Variant
Dim sDayName As String
Dim sMonthDay As String
Dim sYearTime As String
Dim sMonth As String
Dim sDay As String
Dim sYear As String
Dim sTime As StringPrivate Sub Cmd_Connect_Click()
Winsock1.Connect
StatusBar1.SimpleText = Winsock1.State
End SubPrivate Sub Cmd_SetTime_Click()
Cmd_Connect_Click 'refresh the time
aDateTime = Split(Trim(Txt_DateTime.Text), ",")
'
sDayName = aDateTime(0)
sMonthDay = aDateTime(1)
sYearTime = aDateTime(2)
'
aMonthDay = Split(Trim(sMonthDay), " ")
sMonth = aMonthDay(0)
sDay = aMonthDay(1)
'
aYearTime = Split(Trim(sYearTime), " ")
sYear = aYearTime(0)
sTime = aYearTime(1)
sTime = Left(sTime, InStr(1, sTime, "-") - 1) 'remove -EST
' set the system date using day/month/year
Date = sDay & "/" & sMonth & "/" & sYear
Time = sTime
End SubPrivate Sub Form_Load()
Winsock1.RemoteHost = "unimelb.edu.au" 'use the address or IP for your server
Winsock1.RemotePort = 13
End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Cmd_SetTime.Enabled = True
Winsock1.GetData sLocalTime
Txt_DateTime.Text = Trim(sLocalTime)
Winsock1.Close
End Sub
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 ti As TIME_OF_DAY_INFO
Private Type HiLoInt
loInt As Integer
hiInt As Integer
End TypeDim TwoInt As HiLoIntPrivate Type LongType
l As Long
End TypeDim lLong As LongTypePrivate Declare Function NetRemoteTOD Lib "netapi32.dll" (ServerName As Any, buffer As Any) As LongPrivate Declare Function DataFromPtr Lib "kernel32" Alias "lstrcpynW" (RetVal As Any, ByVal Ptr As Long, ByVal nCharCount As Long) As LongPrivate Declare Function NetAPIBufferFree Lib "netapi32.dll" Alias "NetApiBufferFree" (ByVal Ptr As Long) As Long
Private Sub Command1_Click()
Dim sName As String
Dim pbServer() As Byte
Dim ptmpBuffer As Long Dim lRetVal As Long
''Set the NT server name or IP
sName = "\\zhengnanzn" ''Convert the name to a Unicode byte array
pbServer = sName & vbNullChar ''Call the NetRemoteTOD function
lRetVal = NetRemoteTOD(pbServer(0), ptmpBuffer) If lRetVal <> 0 Then
''Function failed
Exit Sub
End If ''Extract the information into a TIME_OF_DAY_INFO structure ''Get first element of the structure
lRetVal = DataFromPtr(ti.tod_elapsedt, ptmpBuffer, 4)
''Get second element of the structure
lRetVal = DataFromPtr(ti.tod_msecs, ptmpBuffer + 4, 4)
''Get third element of the structure
lRetVal = DataFromPtr(ti.tod_hours, ptmpBuffer + 8, 4)
''....
lRetVal = DataFromPtr(ti.tod_mins, ptmpBuffer + 12, 4)
lRetVal = DataFromPtr(ti.tod_secs, ptmpBuffer + 16, 4)
lRetVal = DataFromPtr(ti.tod_hunds, ptmpBuffer + 20, 4)
lRetVal = DataFromPtr(ti.tod_timezone, ptmpBuffer + 24, 4)
lRetVal = DataFromPtr(ti.tod_tinterval, ptmpBuffer + 28, 4)
lRetVal = DataFromPtr(ti.tod_day, ptmpBuffer + 32, 4)
lRetVal = DataFromPtr(ti.tod_month, ptmpBuffer + 36, 4)
lRetVal = DataFromPtr(ti.tod_year, ptmpBuffer + 40, 4)
''Get last element of the structure
lRetVal = DataFromPtr(ti.tod_weekday, ptmpBuffer + 44, 4)
''Convert to local time
If ti.tod_timezone <> -1 Then
ti.tod_hours = ti.tod_hours - ti.tod_timezone \ 60
End If ''Release the memory, allocated by NetRemoteTOD function
NetAPIBufferFree ptmpBuffer MsgBox "Time: " & ti.tod_hours & ":" & ti.tod_mins & ":" & ti.tod_secs
MsgBox "Date: " & ti.tod_day & "/" & ti.tod_month & "/" & ti.tod_year
End Sub