« 解决XMLHTTP获取网页中文乱码问题新兴域名批量查询工具 v2.3.1 »

VB 实现获得标准北京时间

主要是通过http://www.time.ac.cn/timeflash.asp?user=flash这个网站获得时间,获取到的源码是XML格式的,按理来说用Microsoft.XMLDOM来分析比较方便,不过俺对这个组件不太熟悉,还是直接用正则好了。呵呵~
以下代码通过获取源码然后用正则提取出时、分、秒……等。

VBScript code复制代码
Private Function GetHtml(Url As String)
    Dim xmlHttp As Object
    Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
    xmlHttp.open "GET", Url, True
    xmlHttp.send (Null)
    While xmlHttp.ReadyState <> 4
        DoEvents
    Wend
    GetHtml = xmlHttp.responseText
End Function

Private Function getTime() As Date
    Dim Regex As Object, ms As Object, m As Object
    Dim HTML As String
    HTML = GetHtml("http://www.time.ac.cn/timeflash.asp?user=flash")
    Set Regex = CreateObject("VBSCRIPT.REGEXP")
    Regex.IgnoreCase = True
    Regex.Pattern = "<year>(\d+)</year><month>(\d+)</month><day>(\d+)</day>.+?<hour>(\d+)</hour><minite>(\d+)</minite><second>(\d+)</second>"
    Set ms = Regex.Execute(HTML)
    If ms.Count = 0 Then
        getTime = Now()
    Else
        Dim t As String
        t = ms.Item(0).SubMatches(0) _
        & "-" & ms.Item(0).SubMatches(1) _
        & "-" & ms.Item(0).SubMatches(2) _
        & " " & ms.Item(0).SubMatches(3) _
        & ":" & ms.Item(0).SubMatches(4) _
        & ":" & ms.Item(0).SubMatches(5)
        If IsDate(t) Then getTime = CDate(t) Else getTime = Now()
    End If
End Function

如果你机子没有联网的活会直接返回你的系统日期时间。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Copyright © 2009-2011 linhaibo.com. All Rights Reserved .