vb.net网络日期 vbnet日期比较

vb.net获取网络时间失败

是不是另一台电脑是vista 及以上版本的系统 ?

创新互联公司服务项目包括下花园网站建设、下花园网站制作、下花园网页制作以及下花园网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,下花园网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到下花园省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

用管理员模式。试试

Vb.net 2008 如何获取网络时间

你是指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("检测不到网络标准时间服务器time.nist.gov!")

Else

NetTime("time.nist.gov")

If TimeFromNet = "" Then

MsgBox("网络标准时间服务器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

'使网络误差时间小,第2次再中科院国家授时中心时间

NetTime("")

If TimeFromNet = "" Then

MsgBox("网络标准时间服务器超时!")

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

vb 获取网络时间

下列代码不用任何控件就能从国家授时中心网页获取时间获得网络时间。

Function NetTime(Optional url As String) As String '返回包括时间和日期的字符串

Dim obj, OBJStatus, Retrieval

Dim GetText As String

Dim i As Long

Dim myDate As Date

Set Retrieval = CreateObject("Microsoft.XMLHTTP")

If url = "" Then

url = "" '从国家授时中心网页获取时间

End If

'通过下载网页头信息获取网络时间

On Error Goto ToExit

With Retrieval

.Open "Get", url, False, "", ""

.setRequestHeader "If-Modified-Since", "0"

.setRequestHeader "Cache-Control", "no-cache"

.setRequestHeader "Connection", "close"

.Send

If .Readystate 4 Then Exit Function

GetText = .getAllResponseHeaders()

i = InStr(1, GetText, "date:", vbTextCompare)

If i 0 Then '网页下载成功

i = InStr(i, GetText, ",", vbTextCompare)

GetText = Trim(Mid(GetText, i + 1))

i = InStr(1, GetText, " GMT", vbTextCompare)

GetText = Left(GetText, i - 1)

myDate = GetText '字符串变为时间类型

myDate = myDate + #8:00:00 AM# '将时间转化为北京时间

NetTime = myDate '将时间转化为字符串

End If

End With

ToExit:

Set Retrieval = Nothing

Set OBJStatus = Nothing

Set obj = Nothing

End Function

利用上述NetTime函数,可以将本机时间同步到标准时间,误差一般不超过1秒,如果多次运行或加上网络延时校正代码可进一步减少误差。

运行代码后,可以用第三方软件或到国家授时中心网站查看本机时间与标准时间的误差以验证代码的效果,当然更可以用第三方软件来校正电脑时间,这样误差将不超过0.1秒。这是VB中用Time语句设定本机时间无法实现的,因为Time语句的“分辨率”只能达到整秒。

Sub UpDateTime()

Dim sTime as String

sTime=NetTime()

On Error Resume Next

If Stime"" Then

Time=sTime

Date=sTime

End If

End Sub


分享文章:vb.net网络日期 vbnet日期比较
地址分享:http://myzitong.com/article/hppdhe.html