Tôi sửa lại code bài #28 như sau. Bạn nào test thì xin cho biết kết quả. Chỉ xét Windows >= XP
code
Gọi cho Việt Nam
code
Mã:
Sub SetDateTimeFormNet(ByVal zone_offset As Long, ByVal zone_name As String)
Dim GMT_Time As String, currDateTime As String, currDate As Date, currTime As Date
Dim http As Object, shell As Object, sCmd As String, sMonth, s As String, index As Long
Const GMTTime As String = "http://wwp.greenwichmeantime.com/time/scripts/clock-8/runner.php?tz=gmt"
sMonth = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
On Error Resume Next
s = Application.OperatingSystem
index = InStr(1, s, "NT")
If index > 0 Then index = Mid(s, index + 3, 1)
If index > 5 Then
sCmd = "cmd.exe /c TZUTIL /s " & zone_name
ElseIf index = 5 Then
sCmd = "cmd.exe /c rundll32.exe shell32.dll,Control_RunDLL timedate.cpl,,/Z " & zone_name
Else
Exit Sub
End If
Set shell = CreateObject("WScript.Shell")
shell.Run sCmd, 0, True
Set http = CreateObject("Microsoft.XMLHTTP")
http.Open "GET", GMTTime & Now(), False, "", ""
http.send
GMT_Time = http.getResponseHeader("Date")
GMT_Time = Mid$(GMT_Time, 6, Len(GMT_Time) - 9)
For index = 0 To 11
s = Mid(GMT_Time, 3, 5)
If InStr(1, s, sMonth(index)) > 0 Then
GMT_Time = Replace(GMT_Time, s, Application.International(xlDateSeparator) & _
Format(index + 1, "00") & Application.International(xlDateSeparator))
Exit For
End If
Next
currDateTime = DateAdd("h", zone_offset, GMT_Time)
currDate = DateValue(currDateTime)
currTime = Format(TimeValue(currDateTime), "hh:mm:ss")
shell.Run "%comspec% /c time " & currTime, False
shell.Run "%comspec% /c date " & currDate, False
Set shell = Nothing
Set http = Nothing
End Sub
Gọi cho Việt Nam
Mã:
SetDateTimeFormNet 7, "SE Asia Standard Time"