Click here to Skip to main content
15,909,591 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi I want to get real time and date from intrenet not at my pc time how can I do it?

for example. past this code in a notepad and save it as html page now run this html page. if you'll be online then you can see the date(Iran). We can see the date but I want to get it in my app without load any web page.
HTML
<html  >
<head>
    <title>Untitled Page</title>
</head>
<body>
<iframe src="http://free.timeanddate.com/clock/i33dnb3p/n246/tt1/tw0/tm3/td2" frameborder="0" width="82" height="18"></iframe>
</body>
</html>
Posted
Updated 3-May-12 11:54am
v5
Comments
Sergey Alexandrovich Kryukov 1-May-12 17:44pm    
What, is your MSDN broken? How about Google? Did they ban you? Then Bing. What, Microsoft banned you, too? Too bad...
--SA
Akinmade Bond 13-Oct-12 6:52am    
:omg:
Sergey Alexandrovich Kryukov 1-May-12 17:44pm    
Reason for my vote of 1
Too lazy to search for very basic APIs.
--SA

I have code, only in C# as I do not have VB installed. I hope this helps. Interesting question, I have learned something today :)

Console application is as follows:
C#
static void Main(string[] args)
{
    //Iran coordinates
    string latitude = "32.4917";
    string longitude = "53.6167";

    string uri = string.Format(
       "http://www.earthtools.org/timezone/{0}/{1}", latitude, longitude);
    string xml = PostRequest(uri, string.Empty, MediaTypeNames.Text.Html);
    
    timezone instance = Deserialize(xml);
    DateTime localTime = instance.GetLocalTime();

    Console.WriteLine(
       "The date and time is : " + localTime.ToString("dd MMM yyyy HH:mm:ss"));
    Console.Read();
}

private static timezone Deserialize(string xmlText)
{
    using (MemoryStream memStream =
       new MemoryStream(Encoding.UTF8.GetBytes(xmlText)))
    {
        XmlSerializer ser = new XmlSerializer(typeof(timezone));
        return (timezone)ser.Deserialize(memStream);
    }
}

private static string PostRequest(string uri, string msgBody, string contentType)
{
    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(uri);
    webReq.Timeout = 60 * 1000;
    webReq.Method = "POST";
    webReq.ContentType = contentType;
    webReq.ContentLength = msgBody.Length;

    using (StreamWriter streamOut =
       new StreamWriter(webReq.GetRequestStream(), System.Text.Encoding.ASCII))
    {
        streamOut.Write(msgBody);
        streamOut.Flush();
        streamOut.Close();
    }

    string response = null;
    using (StreamReader streamIn =
       new StreamReader(webReq.GetResponse().GetResponseStream()))
    {
        response = streamIn.ReadToEnd();
        streamIn.Close();
    }

    return response;
}


The definition for the class timezone is as follows:

C#
[Serializable()]
public class GpsCoordinates
{
    public string latitude { get; set; }
    public string longitude { get; set; }
}

[Serializable()]
public class timezone
{
    public string version { get; set; }
    public GpsCoordinates location { get; set; }
    public string offset { get; set; }
    public string suffix { get; set; }
    public string localtime { get; set; }
    public string isotime { get; set; }
    public string utctime { get; set; }
    public string dst { get; set; }

    public DateTime GetLocalTime()
    {
        return DateTime.Parse(localtime);
    }
}


Output is as follows:

The date and time is : 04 May 2012 02:46:01
 
Share this answer
 
Use the HttpWebRequest class to download the web page 'www.worldtimeserver.com/current_time_in_IR.aspx'. You can then parse the HTML text from the site you have downloaded and search for the HTML tag containing the time....

XML
<div id="analog-digital">
   <span class="font7">16:42</span>
</div>


:)
 
Share this answer
 
Comments
barbodsoft 3-May-12 10:52am    
I didn't understand . could you explain it with a vb.net code please. and read the solution 4 too.
szeeze 3-May-12 14:33pm    
I apologise. I see that you already attempted the method that I mentioned above. Your comment - 'I don't wont to load a page in my app'

There is a web service that you may use...

http://www.earthtools.org/webservices.htm
barbodsoft 3-May-12 14:51pm    
there are a lot webservices. but I don't know how can I use those without load any web page?

do you have a code that do that for me?
Given that you have not specified any programming language we can only guess what you are trying to do. However you can set the time zone in advance of calling time and date routines that provide local time; i.e. UTC time adjusted to a specific time zone. Take a look at the relevant time methods for the language or API that you are using.
 
Share this answer
 
Comments
barbodsoft 1-May-12 16:55pm    
I want a real time and date even if my pc time and date be wrong.
Richard MacCutchan 1-May-12 17:18pm    
What do you mean real time? The only way to get the time other than from your PC is to use one of the time servers that exist around the world; Google for "time server" and see which one would be useful to you.
barbodsoft 2-May-12 13:05pm    
How can I get the time from google?
barbodsoft 1-May-12 16:57pm    
I programing with vb.net
barbodsoft 2-May-12 12:57pm    
my pc time and date might be wrong. but I want to get right time and date from time zone or other way.

sorry my english is not very good.

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