C sharp - Obtener Fecha correcta

 
Vista:
sin imagen de perfil

Obtener Fecha correcta

Publicado por Francisco (4 intervenciones) el 11/01/2017 19:02:31
Estimados tengo una duda, como puedo obtener la fecha correcta, me explico si cambio la fecha del sistema de windows poder obtener la fecha correcta aunque se cambien en windows, saludos.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 356
Plata
Ha disminuido 1 puesto en C sharp (en relación al último mes)
Gráfica de C sharp

Obtener Fecha correcta

Publicado por Miguel (160 intervenciones) el 11/01/2017 23:23:26
La mejor forma sería obtener la fecha de internet.

http://stackoverflow.com/questions/6676191/how-can-get-datetime-from-internet-external-resource-not-from-server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public static DateTime GetFastestNISTDate()
{
    var result = DateTime.MinValue;
 
    // Initialize the list of NIST time servers
    // http://tf.nist.gov/tf-cgi/servers.cgi
    string[] servers = new string[] {
        "nist1-ny.ustiming.org",
        "nist1-nj.ustiming.org",
        "nist1-pa.ustiming.org",
        "time-a.nist.gov",
        "time-b.nist.gov",
        "nist1.aol-va.symmetricom.com",
        "nist1.columbiacountyga.gov",
        "nist1-chi.ustiming.org",
        "nist.expertsmi.com",
        "nist.netservicesgroup.com"
};
 
        // Try 5 servers in random order to spread the load
        Random rnd = new Random();
        foreach (string server in servers.OrderBy(s => rnd.NextDouble()).Take(5))
        {
            try
            {
                // Connect to the server (at port 13) and get the response
                string serverResponse = string.Empty;
                using (var reader = new StreamReader(new System.Net.Sockets.TcpClient(server, 13).GetStream()))
                {
                    serverResponse = reader.ReadToEnd();
                }
 
                // If a response was received
                if (!string.IsNullOrEmpty(serverResponse))
                {
                    // Split the response string ("55596 11-02-14 13:54:11 00 0 0 478.1 UTC(NIST) *")
                    string[] tokens = serverResponse.Split(' ');
 
                    // Check the number of tokens
                    if (tokens.Length >= 6)
                    {
                        // Check the health status
                        string health = tokens[5];
                        if (health == "0")
                        {
                            // Get date and time parts from the server response
                            string[] dateParts = tokens[1].Split('-');
                            string[] timeParts = tokens[2].Split(':');
 
                            // Create a DateTime instance
                            DateTime utcDateTime = new DateTime(
                                Convert.ToInt32(dateParts[0]) + 2000,
                                Convert.ToInt32(dateParts[1]), Convert.ToInt32(dateParts[2]),
                                Convert.ToInt32(timeParts[0]), Convert.ToInt32(timeParts[1]),
                                Convert.ToInt32(timeParts[2]));
 
                            // Convert received (UTC) DateTime value to the local timezone
                            result = utcDateTime.ToLocalTime();
 
                            return result;
                            // Response successfully received; exit the loop
 
                        }
                    }
 
                }
 
            }
            catch
            {
                // Ignore exception and try the next server
            }
        }
        return result;
    }
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil

Obtener Fecha correcta

Publicado por Francisco (4 intervenciones) el 11/01/2017 23:40:58
De la misma forma que sugieres ya lo había pensado, creía que había alguna otra forma que no fuera de un servidor de Internet, pero de todas maneras agradezco tu ayuda , muchas gracias, saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 356
Plata
Ha disminuido 1 puesto en C sharp (en relación al último mes)
Gráfica de C sharp

Obtener Fecha correcta

Publicado por Miguel (160 intervenciones) el 12/01/2017 02:02:19
Desconozco si hay otra forma...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar