Foxtable(狐表)用户栏目专家坐堂 → 在单机上 如何获取准确的北京日期时间,而不受本地机的系统日期时间影响?


  共有4387人关注过本帖树形打印复制链接

主题:在单机上 如何获取准确的北京日期时间,而不受本地机的系统日期时间影响?

帅哥哟,离线,有人找我吗?
jswjyjf
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:九尾狐 帖子:2552 积分:20057 威望:0 精华:0 注册:2012/5/19 11:57:00
在单机上 如何获取准确的北京日期时间,而不受本地机的系统日期时间影响?  发帖心情 Post By:2014/7/30 9:57:00 [只看该作者]

在单机上 如何获取准确的北京日期时间,而不受本地机的系统日期时间影响?

 回到顶部
帅哥哟,离线,有人找我吗?
Bin
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:35433 积分:178524 威望:0 精华:3 注册:2013/3/30 16:36:00
  发帖心情 Post By:2014/7/30 9:58:00 [只看该作者]


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/7/30 9:58:00 [只看该作者]


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/7/30 10:07:00 [只看该作者]

 改一下,增加成功率

 

Dim TimeServers As String(,) = New String(2, 1) {}
TimeServers(0, 0) = "time-b.timefreq.bldrdoc.gov"
TimeServers(0, 1) = "132.163.4.102"
TimeServers(1, 0) = "time-c.timefreq.bldrdoc.gov"
TimeServers(1, 1) = "132.163.4.103"
TimeServers(2, 0) = "utcnist.colorado.edu"
TimeServers(2, 1) = "128.138.140.44"

Dim portNum As Integer = 13
Dim hostName As String
Dim bytes As Byte() = New Byte(1023) {}
Dim bytesRead As Integer = 0
Dim client As New System.Net.Sockets.TcpClient()
Dim str1 As String
Dim count As Integer = 0
Do
    For i As Integer = 0 To 2
        hostName = TimeServers(i, 1)
        Try
            client.Connect(hostName, portNum)
            Dim ns As System.Net.Sockets.NetworkStream = client.GetStream()
            bytesRead = ns.Read(bytes, 0, bytes.Length)
            client.Close()
           
            Exit For
        Catch generatedExceptionName As System.Exception
        End Try
    Next
    str1 = System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRead)
    count += 1
Loop While count < 10 AndAlso str1 Is Nothing
Dim sp As Char() = New Char(0) {}
sp(0) = " "c
Dim dt As System.DateTime = New DateTime()

Dim s As String()
s = str1.Split(sp)
dt = System.DateTime.Parse(s(1) & " " & s(2))
'得到标准时间
dt = dt.AddHours(8)
'得到北京时间
Messagebox.show( dt)


 回到顶部
帅哥哟,离线,有人找我吗?
hunanwl
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:447 积分:3759 威望:0 精华:0 注册:2012/12/9 8:52:00
  发帖心情 Post By:2014/9/18 22:16:00 [只看该作者]

 改一下,增加成功率

 

Dim TimeServers As String(,) = New String(2, 1) {}
TimeServers(0, 0) = "time-b.timefreq.bldrdoc.gov"
TimeServers(0, 1) = "132.163.4.102"
TimeServers(1, 0) = "time-c.timefreq.bldrdoc.gov"
TimeServers(1, 1) = "132.163.4.103"
TimeServers(2, 0) = "utcnist.colorado.edu"
TimeServers(2, 1) = "128.138.140.44"

Dim portNum As Integer = 13
Dim hostName As String
Dim bytes As Byte() = New Byte(1023) {}
Dim bytesRead As Integer = 0
Dim client As New System.Net.Sockets.TcpClient()
Dim str1 As String
Dim count As Integer = 0
Do
    For i As Integer = 0 To 2
        hostName = TimeServers(i, 1)
        Try
            client.Connect(hostName, portNum)
            Dim ns As System.Net.Sockets.NetworkStream = client.GetStream()
            bytesRead = ns.Read(bytes, 0, bytes.Length)
            client.Close()
            
            Exit For
        Catch generatedExceptionName As System.Exception
        End Try
    Next
    str1 = System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRead)
    count += 1
Loop While count < 10 AndAlso str1 Is Nothing
Dim sp As Char() = New Char(0) {}
sp(0) = " "c
Dim dt As System.DateTime = New DateTime()

Dim s As String()
s = str1.Split(sp)
dt = System.DateTime.Parse(s(1) & " " & s(2))
'得到标准时间
dt = dt.AddHours(8)
'得到北京时间
Messagebox.show( dt)





看附件

多运行几次就会出错啊   说是索引超出数组界限啊

在狐表中是这样

让后我转到vs中  也是这样


甜甜老师   再看下你的代码是什么问题
[此贴子已经被作者于2014-9-18 22:26:54编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/9/18 22:33:00 [只看该作者]

Dim TimeServers As String(,) = New String(2, 1) {}
TimeServers(0, 0) = "time-b.timefreq.bldrdoc.gov"
TimeServers(0, 1) = "132.163.4.102"
TimeServers(1, 0) = "time-c.timefreq.bldrdoc.gov"
TimeServers(1, 1) = "132.163.4.103"
TimeServers(2, 0) = "utcnist.colorado.edu"
TimeServers(2, 1) = "128.138.140.44"

Dim portNum As Integer = 13
Dim hostName As String
Dim bytes As Byte() = New Byte(1023) {}
Dim bytesRead As Integer = 0
Dim client As New System.Net.Sockets.TcpClient()
Dim str1 As String
Dim count As Integer = 0
Do
    For i As Integer = 0 To 2
        hostName = TimeServers(i, 1)
        Try
            client.Connect(hostName, portNum)
            Dim ns As System.Net.Sockets.NetworkStream = client.GetStream()
            bytesRead = ns.Read(bytes, 0, bytes.Length)
            client.Close()
           
            Exit For
        Catch generatedExceptionName As System.Exception
        End Try
    Next
    str1 = System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRead)
    count += 1
Loop While count < 10 AndAlso str1 Is Nothing
If str1 Is Nothing Then
    msgbox("网络繁忙,请稍后再试")
Else
    Dim sp As Char() = New Char(0) {}
    sp(0) = " "c
    Dim dt As System.DateTime = New DateTime()
   
    Dim s As String()
    s = str1.Split(sp)
    dt = System.DateTime.Parse(s(1) & " " & s(2))
    '得到标准时间
    dt = dt.AddHours(8)
    '得到北京时间
    Messagebox.show( dt)
End If


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/9/18 22:33:00 [只看该作者]

网络问题,有时候会获取不到的。

 回到顶部