Click here to Skip to main content
16,015,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How can i get the Universal Time without using my System Time.

For example, If my system time is set to some wrong time, then in the code DateTime.UTCNow is giving me wrong UTC time as well.

Hence I need to get the correct UTC time even my system time is set wrong. Pls help


Thanking you...

MSK
Posted
Comments
johannesnestler 16-Jul-14 7:08am    
OriginalGriffs answer is the way to go (or setting up your own "time-Service/Server), but your question alone shows lack of understanding how "time" works in computing (you expected UTCNow to make an external system call magically?).
So in addition to you current problem it could not be a bad idea to do some "research" in this field.

The only way to do that is to use an internet time service to return the actual time - any other approach will fail because has to use the PC time.

There are a couple of approaches to do this, but they all involve talking to a NIST: http://stackoverflow.com/questions/6435099/c-how-to-get-datetime-from-the-internet[^]
 
Share this answer
 
C#
string host = Dns.GetHostName();
var client = new TcpClient(host, 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);
    TextBox1.Text = localDateTime.ToString();
}
 
Share this answer
 
OGs answer is the correct one if you are connected to the internet.

If not then there is another way if you have a gps. Gps devices receive data containing very accurate UTC time because the satellite clocks are synchronised to very accurate atomic clocks.. If you set gps to NMEA data for example you can parse the time value from the data.

This method can be used in remote locations to set the PC clock accurately. See here for example:
Writing Your Own GPS Applications: Part I[^]
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900