首页 > 解决方案 > 通过网站获取当前日期/时间

问题描述

通过 Access / VBA 代码我想从世界时间服务器捕获当前日期,如果有人知道方法吗?

标签: vbams-accesstime

解决方案


谢谢,我设法执行。我添加了参考:Microsoft XML,v6.0,并且我能够使用下面的代码成功运行。非常感谢大家。

Public Function DateUtcNow() As Date

' ServiceUrl for time service.

Const ServiceUrl        As String = "http://worldclockapi.com/api/json/est/now"


' Engine to communicate with the service.
Dim XmlHttp             As XMLHTTP60
Dim ResponseText        As String
Dim txdata As Date

On Error GoTo Err_DateUtcNow

Set XmlHttp = New XMLHTTP60

XmlHttp.Open "GET", ServiceUrl, Async

XmlHttp.send

ResponseText = XmlHttp.ResponseText
If Not ResponseText Like "*currentdatetime*" Then txdata = ""
txdata = Mid(ResponseText, InStr(ResponseText, "currentdatetime") + 18, 10)
'MsgBox txdata
MsgBox Format(CDate(txdata), "dd/mm/yyyy")

Exit_DateUtcNow:
Set XmlHttp = Nothing
Exit Function

Err_DateUtcNow:
MsgBox "Error" & Str(Err.Number) & ": " & Err.Description, vbCritical + vbOKOnly, "Web Service Error"
Resume Exit_DateUtcNow
End Function

推荐阅读