none
Buscar data e Hora não do computador. RRS feed

  • Pergunta

  • Pessoal, preciso salvar data e hora no banco só que preciso pegar tipo de algum web service que busque data e hora de brasília, não quero pegar do computador.

    Preciso ter certeza que a data e hora é a correta.

     

    alguem sabe como me ajudar?

    quarta-feira, 12 de setembro de 2018 11:04

Todas as Respostas

  • TimeZoneInfo kstZone = TimeZoneInfo.FindSystemTimeZoneById("E. South America Standard Time"); // Brasilia/BRA
                            DateTime dateTimeBrasilia = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, kstZone);
                            inphorario.Text = dateTimeBrasilia.ToString();

    NAO FUNCIONA. ALTEREI A DATA DO COMPUTADOR E PEGOU.. 


    quarta-feira, 12 de setembro de 2018 11:28
  • Alan Ribas,

        Veja isso:

    =================================================
    How to get DateTime from the internet?

    public static DateTime GetNistTime()
    {
        DateTime dateTime = DateTime.MinValue;
    
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi?lzbc=siqm9b");
        request.Method = "GET";
        request.Accept = "text/html, application/xhtml+xml, */*";
        request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
        request.ContentType = "application/x-www-form-urlencoded";
        request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No caching
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            StreamReader stream = new StreamReader(response.GetResponseStream());
            string html = stream.ReadToEnd();//<timestamp time=\"1395772696469995\" delay=\"1395772696469995\"/>
            string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
            double milliseconds = Convert.ToInt64(time) / 1000.0;
            dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();
        }
    
        return dateTime;
    }


          ----------- xxxxx -----------

    NIST Internet Time Service

    var client = new TcpClient("time.nist.gov", 13);
    using (var streamReader = new StreamReader(client.GetStream()))
    {
        var response = streamReader.ReadToEnd();
        var utcDateTimeString = response.Substring(7, 17);
        var localDateTime = DateTime.ParseExact(utcDateTimeString, "yy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
    }

    https://stackoverflow.com/questions/6435099/how-to-get-datetime-from-the-internet
    =================================================

    Daytime, Internet Time Service Class

    https://www.codeproject.com/articles/5972/daytime-internet-time-service-class
    =================================================
    Servidor de data e hora na internet

    https://social.msdn.microsoft.com/Forums/pt-BR/e6d8e284-623c-4f61-b3b2-28e12bdd9b48/servidor-de-data-e-hora-na-internet
    =================================================

    []'s,
    Fabio I.

    quarta-feira, 12 de setembro de 2018 12:33