Results 1 to 6 of 6

Thread: [RESOLVED] Current time form the net server or UTC time

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    18

    Resolved [RESOLVED] Current time form the net server or UTC time

    Hi,

    I need to get the current time form the internet. For example as I type "time now" in google its returns me my current location (country) time.

    I do not wish to get the system time. All I want is to somehow get the time from the internet. I searched through the net but couldn't find working code to achieve this. any help would be appreciated.

    If anyhow I could get the UTC time I can convert it into my time zone.

    So, is there a away to get the UTC time?

    thank you!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Current time form the net server or UTC time

    i have the code to do this i do not remember where it came from and i have never simplyfyed the modules, if i post them here they may not make much sense i will see if can find something easier
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Current time form the net server or UTC time

    google found i had answered this before http://www.vbforums.com/showthread.p...73#post3691873

    but the code there, no longer works in XP, don't know about other os versions

    Code:
    Function InternetTime() As Date
    
        '-----------------------------------------------------------------------------------
        'This function returns the Greenwich Mean Time retrieved from an internet server.
        'You can use the optional argument GMTDifference in order to add (or subtract)
        'an hour from the GMT time. For Example if you call the function as:
        '=InternetTIme(2) it will return the (local) hour GMT + 2. Note that the
        'GMTDifference variable is an integer number.
       
        'Written by:    Christos Samaras
        'Date:          25/09/2013
        'Last Updated:  10/01/2017
        'e-mail:        xristos.samaras@gmail.com
        'site:          http://www.myengineeringworld.net
        '-------------------------------------------------------------------------------
    
        'Declaring the necessary variables.
        Dim Request     As object
        Dim ServerURL   As String
        Dim Results     As String
        
        ServerURL = "https://time.is/UTC"
        'Build the XMLHTTP object and check if was created successfully.
        On Error Resume Next
        Set Request = CreateObject("MSXML2.ServerXMLHTTP.6.0")
        If Err.Number <> 0 Then
            Exit Function
        End If
        On Error GoTo 0
       
        'Create the request.
        Request.Open "GET", ServerURL, False, "", ""
       
        'Send the request to the internet server.
        Request.Send
       
        'Based on the status node result, proceed accordingly.
        If Request.ReadyState = 4 Then
           
            'If the request succeed, the following line will return
             With CreateObject("htmlfile")
                .body.innerhtml = Request.responseText
                d = Split(.getelementbyid("dd").innertext, ",")
                s = CDate(d(1) & " " & d(2))
                InternetTime = s + CDate(.getelementbyid("twd").innertext)
            End With
       End If
       
        'Release the XMLHTTP object.
        Set Request = Nothing
    
    End Function
    i found this code from the included url, which also did not work in XP, but have fixed and simplified, tested and worked without error, call like
    Code:
    msgbox internettime
    you can the convert the returned date to whatever timezone /format you wish

    NOTE this queries a time website and is probably quite accurate, but it not a NTP server, as in the code linked to above, that i had posted previously
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Current time form the net server or UTC time

    There might also be something useful in the links returned from the one below:

    https://www.google.com/search?hl=en&....0.qedgzFJvwww

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    18

    Re: Current time form the net server or UTC time

    @ westconn1

    Ooo! WOW! Thank you so much brother! I just converted the UTC date/Time your function returned to Local Time and it worked pretty damn accurate

    I checked it couple of times, its giving me a little difference in time in milliseconds when comparing it with my system time which is synchronized with microsoft NTP server - I'm on windows 10.

    I wonder why is there a difference of time in milliseconds if the system time sync form Microsoft or Google NTP server?

    Thank you sooooooooooooo! much

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Current time form the net server or UTC time

    I wonder why is there a difference of time in milliseconds
    could be several reasons, the time it takes for the code to complete from when the time is read, how long since the system time was updated and how accurate the website is are just some
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width