Foxtable(狐表)用户栏目专家坐堂 → [求助]ACCESS数据源可以通过代码获取服务器时间吗


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

主题:[求助]ACCESS数据源可以通过代码获取服务器时间吗

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


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2013/4/6 9:42:00 [显示全部帖子]

将下面的代码加入到全局代码中:

 

 Public Shared Function DataStandardTime() As DateTime
        '返回国际标准时间
        Dim TimeServers As String(,) = New String(3, 1) {}
        TimeServers(0, 0) = "time-a.timefreq.bldrdoc.gov"
        TimeServers(0, 1) = "132.163.4.101"
        TimeServers(1, 0) = "time-b.timefreq.bldrdoc.gov"
        TimeServers(1, 1) = "132.163.4.102"
        TimeServers(2, 0) = "time-c.timefreq.bldrdoc.gov"
        TimeServers(2, 1) = "132.163.4.103"
        TimeServers(3, 0) = "utcnist.colorado.edu"
        TimeServers(3, 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()
        For i As Integer = 0 To 3
            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 Try
            Catch generatedExceptionName As System.Exception
            End Try
        Next
        Dim sp As Char() = New Char(0) {}
        sp(0) = " "c
        Dim dt As System.DateTime = New DateTime()
        Dim str1 As String
        str1 = System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRead)

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

    End Function

 

这样你在任何代码中,都可以通过互联网获取标准北京时间:

 

Dim dt As Date = DataStandardTime

MessageBox.show("当前时间:" & DataStandardTime)

 

 

 


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


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2013/4/6 17:39:00 [显示全部帖子]

这么好的东西,没有人留意啊

 回到顶部