当前位置:编程学习 > VB >>

VB 网络时间问题

怎样获得网络上日期和时间 然后将它付给Text文本
补充:不用这么麻烦吧 还有没有简单一些的
答案:事实上在Internet上有三个不同的时间服务,每一个都由Request for Comment(RFC)定义为Internet日期时间标准。这三个标准分别为:RFC-867、RFC-868和RFC-1305。下面就先介绍RFC-867:RFC867 Daytime协议(RFC867 Daytime Protocol)本RFC规范了一个ARPA Internet community上的标准。在ARPA Internet上的所有主机应当采用和实现这个标准。一个有用的测量和调试工具就是daytime服务。它的作用就是返回当前时间和日期,格式是字符串格式。 * 基于TCP的daytime服务 daytime服务是基于TCP的应用,服务器在TCP端口13侦听,一旦有连接建立就返回ASCII形式的日期和时间(接收到的任何数据被忽略),在传送完后关闭连接。 * 基于UDP的daytime服务 daytime服务也可以使用UDP协议,它的端口也是13,不过UDP是用数据报传送当前时间的。接收到的数据被忽略。 * Daytime格式 对于daytime没有特定的格式,建议使用ASCII可打印字符,空格和回车换行符。daytime应该在一行上。下面是两种流行的格式:一种流行的格式是:Weekday, Month Day, Year Time-Zone例子:Tuesday, February 22, 1982 17:37:43-PST另一种流行的格式用于SMTP中:dd mmm yy hh:mm:ss zzz例子:02 FEB 82 07:59:01 PST 注意:对于机器来说,有用的时间采用了时间协议(Time Protocol RFC-868) 接下来我们用VB程序实现通过RFC867协议设置我们自己的计算机系统时间,为使程序简化,程序未进行日期校正,只进行时间校正。在FORM1中添加1个Winsock控件,将下面代码剪贴到FORM1的代码窗体中即可: Option Explicit'采用RFC867 Daytime协议获取标准时间例程' www.time.ac.cn为中科院国家授时中心,采用北京时间'时间格式:Mon Jul 26 09:58:57 2004'time.nist.gov为美国标准技术院,采用格灵威时间'时间格式:53212 04-07-26 02:00:12 50 0 0 488.3 UTC(NIST) *Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Dim NoSrv As BooleanDim TimeFromNet Private Sub Form_Load()Winsock1.Protocol = sckTCPProtocol '采用TCP协议NetTime " www.time.ac.cn" '首先取中科院国家授时中心时间If NoSrv Or TimeFromNet = "" Then'若未取到中科院国家授时中心时间,则取美国标准技术院时间NetTime "time.nist.gov"If NoSrv Or TimeFromNet = "" Then'若不能取美国标准技术院时间,则报错MsgBox "检测不到网络标准时间服务器time.nist.gov!"Else'为使网络传输误差减小,第2次再取美国标准技术院时间NetTime "time.nist.gov"If TimeFromNet = "" ThenMsgBox "网络标准时间服务器time.nist.gov超时!"ElseTimeFromNet = Mid(TimeFromNet, 17, 8)TimeFromNet = TimeSerial((Hour(TimeFromNet) + 8) Mod 24, Minute(TimeFromNet), Second(TimeFromNet))Time = TimeFromNet '设置系统时间End IfEnd IfElse'为使网络传输误差减小,第2次再取中科院国家授时中心时间NetTime " www.time.ac.cn"If TimeFromNet = "" ThenMsgBox "网络标准时间服务器 www.time.ac.cn超时!"ElseTime = Mid(TimeFromNet, 12, 8) '设置系统时间End IfEnd IfEndEnd Sub '关闭Winsock子程序Private Sub Winsock1_Close()If Winsock1.State <> sckClosed ThenWinsock1.CloseEnd IfEnd Sub 'Winsock接收数据事件Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)TimeFromNet = String(bytesTotal, " ")Winsock1.GetData TimeFromNet, vbString, bytesTotalEnd Sub 'Winsock出错事件Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)NoSrv = TrueEnd Sub '从互联网上标准时间提供网站获取标准时间Private Sub NetTime(TimeSrv As String)NoSrv = FalseTimeFromNet = ""If Winsock1.State <> sckClosed Then Winsock1.CloseWinsock1.RemoteHost = TimeSrv ' " www.time.ac.cn" 或 "time.nist.gov"Winsock1.RemotePort = 13Winsock1.LocalPort = 0Winsock1.ConnectDo While TimeFromNet = "" '循环等待标准时间网站返回时间数据If NoSrv Then Exit Do '若Winsock出错,则跳出循环等待Sleep 55DoEventsLoopIf Winsock1.State <> sckClosed Then Winsock1.CloseEnd Sub
看看是不是你想要的

Private Declare Function GetTickCount Lib "kernel32" () As Long
Public TStart, TNow, TLast, TMinute, TSecond, TTotal

Private Sub Command1_Click()
Command1.Enabled = False: Command2.Enabled = True: Command4.Enabled = False
Text1.Text = ""
End Sub

Private Sub Command2_Click()
Command3.Enabled = True
Timer1.Enabled = True
TTotal = TLast + TTotal
TStart = GetTickCount
Call Timer1_Timer
End Sub

Private Sub Command3_Click()
Command1.Enabled = True: Command2.Enabled = False
Command3.Enabled = False: Command4.Enabled = True
Timer1.Enabled = False
TStart = 0: TEnd = 0: LTime = 0: TMinute = 0: TSecond = 0: TLast = 0
End Sub

Private Sub Command4_Click()
End
End Sub

Private Sub Form_Load()
Command1.Caption = "开始": Command2.Caption = "计时": Command3.Caption = "停止": Command4.Caption = "退出"
Command1.Default = True: Command2.Enabled = False: Command3.Enabled = False
Timer1.Interval = 1000: Timer1.Enabled = False
Timer2.Interval = 1000
TStart = 0: TEnd = 0: LTime = 0: TMinute = 0: TSecond = 0: TLast = 0
End Sub

Private Sub Timer1_Timer()
TNow = GetTickCount
TLast = Val(TNow - TStart) \ 1000
TMinute = TLast \ 60
TSecond = TLast Mod 60
Text1.Text = "距你本次点击[计时]的时间为" & TMinute & ":" & TSecond & vbCrLf & "从第一次点击[计时]到本次点击经过了:" & TTotal & "秒"
End Sub


Private Sub Timer2_Timer()
Label1 = "日期:" & Date & " 时间:" & Time
End Sub
这是显示在网页上 大同小异 你自己看看

上一个:问一个vb的问题
下一个:VB如何用WinIO打字?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,