Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i get the current date and time from internet
in format like this 20/06/2015 10:30:13 AM
I was try that below code,

C#
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);
            Response.Write(response);
            var localDateTime = DateTime.ParseExact(utcDateTimeString, "yy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
            nets = localDateTime.ToString();
        }

working fine but not getting tt[AM or PM].
Posted
Comments
Sreekanth Mothukuru 17-Jun-15 1:58am    
Also include tt at the very end of the format provided like this:

var localDateTime = DateTime.ParseExact(utcDateTimeString, "yy-MM-dd hh:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
venkatesh@India 17-Jun-15 2:00am    
I was try that getting error.

1 solution

That's because the server doesn't return it: it returns a 24 hour value instead of an AM / PM one.
But that's fine, because once you have processed it into a DateTime value it's irrelevant anyway - a DateTime doesn't need to know.

AM/PM is a presentation function - it is only relevant when you convert a DatetIme to a string - and you code is using the default Date setting for your PC to decide how to format the date because you do not specify any particular format:
C#
nets = localDateTime.ToString();

If you want a specific format, you have to say exactly what you want. This may help: Formatting a DateTime for display - format string description[^]

And BTW: I would strongly suggest that you don't trim the NIST string so much: you are stripping out the leading two digits of the year. This means that your code will only work for dates in the current century, so if you later reuse some of that code it could start giving you bad values. the NIST string includes the full year, so I'd string it out complete, and use ParseExact to process "yyyy-MM-dd hh:mm:ss" instead.
 
Share this answer
 
Comments
venkatesh@India 17-Jun-15 2:04am    
Any other solution for getting the AM and PM from other source.Because i want to check that
OriginalGriff 17-Jun-15 2:40am    
AM/PM is just a different representation of the time: it's a 12 hour display of a 24 hour value.
So 13:45 is the same value as 01:45 PM, just shown differently. All you need to do is read the 24 hour value (as you are) and format it to 12 hour format with the AM/PM - the link shows you how.
venkatesh@India 18-Jun-15 7:57am    
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);
Response.Write(response);
var localDateTime = DateTime.ParseExact(utcDateTimeString, "yy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
nets = localDateTime.ToString();
}
Getting error ,
Error:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 132.163.4.102:13
OriginalGriff 18-Jun-15 8:05am    
Well...there isn't anything I can do about that! :laugh:
You could re-try, or increase the TcpClient.ReceiveTimeout Property:
https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.receivetimeout(v=vs.110).aspx
But I can't speed up government websites!
venkatesh@India 18-Jun-15 9:29am    
i will try to set TcpClient.ReceiveTimeout =5000,still same error show.But i can make change to TcpClient.SetTimeout =5000 working fine sometimes shows error.Any other fast server...

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