locked
Update date and time with internet RRS feed

  • Question

  • How can I update the Date and Time of the computer with the internet servers?

    I tried to use this code: http://msdn.microsoft.com/en-us/library/ms172517%28v=vs.90%29.aspx

    But I'm getting 2 hours of difference. I believe that is because here in Brazil we have "daylight saving time".

    I wish my Application could check on internet the correct Data and Time, and force the system to update. Then my records would be saved with the correct values. (I intend to use this values for future updates).


    Valdirnm

    Sunday, December 7, 2014 2:57 PM

Answers

  • How can I update the Date and Time of the computer with the internet servers?

    I tried to use this code: http://msdn.microsoft.com/en-us/library/ms172517%28v=vs.90%29.aspx

    But I'm getting 2 hours of difference. I believe that is because here in Brazil we have "daylight saving time".

    I wish my Application could check on internet the correct Data and Time, and force the system to update. Then my records would be saved with the correct values. (I intend to use this values for future updates).


    Valdirnm

    This is a start (if you're interested in going this way). I'll finish it at some point in the future - again if there's interest.

    I have a class started and the code for it is shown on a page of my website here.

    I tested it thusly:

    Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load ' Test it here... Dim wt As New WorldTime With wt .BingKey = "" '<-- YOUR API KEY GOES HERE .TimeZoneDBKey = "" '<-- YOUR API KEY GOES HERE .BingLocationQueryString = "Charleston, SC" .GetLocalTime_IncludeBingSearch() End With If wt.LocationLocalTime.HasValue AndAlso wt.LocationLocalTimeUTC.HasValue Then Dim sb As New System.Text.StringBuilder sb.AppendLine("Location From Bing: " & wt.LocationNameFromBing) sb.AppendLine("Location From TimeZoneDB: " & wt.LocationNameFromTimeZoneDB) sb.AppendLine() sb.AppendLine("Location Current Time (UTC): " & wt.LocationLocalTimeUTC.Value.ToLongTimeString) sb.AppendLine("Location Current Time: " & wt.LocationLocalTime.Value.ToLongTimeString) MessageBox.Show(sb.ToString, "Values Returned") End If Stop End Sub End Class


    Let me know if you're interested and if so, I'll finish it.

    For what it's worth. :)


    Still lost in code, just at a little higher level.

    :-)

    • Marked as answer by Youjun Tang Tuesday, December 16, 2014 9:58 AM
    Sunday, December 7, 2014 8:05 PM
  • What you are using in the mentioned example is just system time. I don't see the time server connection.

    Here is an example that checks the time on time servers: Get internet time? You also have to remember that because you don't know where your code is going to run, you start with UTC/GMT time and then adjust for the time zone of the local computer.


    • Edited by IrekJ Wednesday, December 10, 2014 3:14 PM
    • Proposed as answer by IrekJ Wednesday, December 10, 2014 3:14 PM
    • Marked as answer by Youjun Tang Tuesday, December 16, 2014 9:58 AM
    Sunday, December 7, 2014 8:45 PM

All replies

  • How can I update the Date and Time of the computer with the internet servers?

    I tried to use this code: http://msdn.microsoft.com/en-us/library/ms172517%28v=vs.90%29.aspx

    But I'm getting 2 hours of difference. I believe that is because here in Brazil we have "daylight saving time".

    I wish my Application could check on internet the correct Data and Time, and force the system to update. Then my records would be saved with the correct values. (I intend to use this values for future updates).


    Valdirnm

    A few years back I put a program together which used data from this website.

    If you'll look specifically at the API they have, it will allow you to get the correct current time for anywhere in the world.

    If I recall correctly, I used Bing Maps (another API based service) to figure out what the user's lat/lon was, then I used that to get the time. You can then programatically set the time if you want.


    Still lost in code, just at a little higher level.

    :-)

    Sunday, December 7, 2014 3:09 PM
  • Do you have the system set up to use 'Internet Time'?  If so you can adjust the frequency of updates if the default is not sufficient.

    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer

    Sunday, December 7, 2014 7:48 PM
  • How can I update the Date and Time of the computer with the internet servers?

    I tried to use this code: http://msdn.microsoft.com/en-us/library/ms172517%28v=vs.90%29.aspx

    But I'm getting 2 hours of difference. I believe that is because here in Brazil we have "daylight saving time".

    I wish my Application could check on internet the correct Data and Time, and force the system to update. Then my records would be saved with the correct values. (I intend to use this values for future updates).


    Valdirnm

    This is a start (if you're interested in going this way). I'll finish it at some point in the future - again if there's interest.

    I have a class started and the code for it is shown on a page of my website here.

    I tested it thusly:

    Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load ' Test it here... Dim wt As New WorldTime With wt .BingKey = "" '<-- YOUR API KEY GOES HERE .TimeZoneDBKey = "" '<-- YOUR API KEY GOES HERE .BingLocationQueryString = "Charleston, SC" .GetLocalTime_IncludeBingSearch() End With If wt.LocationLocalTime.HasValue AndAlso wt.LocationLocalTimeUTC.HasValue Then Dim sb As New System.Text.StringBuilder sb.AppendLine("Location From Bing: " & wt.LocationNameFromBing) sb.AppendLine("Location From TimeZoneDB: " & wt.LocationNameFromTimeZoneDB) sb.AppendLine() sb.AppendLine("Location Current Time (UTC): " & wt.LocationLocalTimeUTC.Value.ToLongTimeString) sb.AppendLine("Location Current Time: " & wt.LocationLocalTime.Value.ToLongTimeString) MessageBox.Show(sb.ToString, "Values Returned") End If Stop End Sub End Class


    Let me know if you're interested and if so, I'll finish it.

    For what it's worth. :)


    Still lost in code, just at a little higher level.

    :-)

    • Marked as answer by Youjun Tang Tuesday, December 16, 2014 9:58 AM
    Sunday, December 7, 2014 8:05 PM
  • What you are using in the mentioned example is just system time. I don't see the time server connection.

    Here is an example that checks the time on time servers: Get internet time? You also have to remember that because you don't know where your code is going to run, you start with UTC/GMT time and then adjust for the time zone of the local computer.


    • Edited by IrekJ Wednesday, December 10, 2014 3:14 PM
    • Proposed as answer by IrekJ Wednesday, December 10, 2014 3:14 PM
    • Marked as answer by Youjun Tang Tuesday, December 16, 2014 9:58 AM
    Sunday, December 7, 2014 8:45 PM