0

I am getting the time from internet using the code

    public static DateTime GetNistTime()
    {
        var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
        var response = myHttpWebRequest.GetResponseAsync();
        string todaysDates = response.Result.Headers["date"];
        return DateTime.ParseExact(todaysDates,
                                   "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
                                   CultureInfo.InvariantCulture.DateTimeFormat,
                                   DateTimeStyles.AssumeUniversal);
    }

I got this code from here (made little changes to run on xamarin).

I am getting the time nicely but what I need is, I want to set the StartTimer to run the clock for every second by taking the above time as input.

I have tried the below code but the clock is not moving.

        DateTime dt = GetNistTime();
        Label timeNow = new Label();

        Device.StartTimer(TimeSpan.FromSeconds(1), () => {
            timeNow.Text = dt.ToUniversalTime().ToString("T");
            return true; });

Any help would be appreciated .. Thanks in advance..

Community
  • 1
  • 1

1 Answers1

0

Looks like you are not making the network calls again to retrieve updated time from the website, so as of you can update the local datetime instance and make this work.

  dt = dt.ToUniversalTime().AddSeconds(1);
  timeNow.Text = dt.ToString("T");

Hope that helps.

Dilmah
  • 1,137
  • 11
  • 14