从互联网(站点)获取当前日期和时间 [英] get curect time and date from intrenet(a site)

查看:87
本文介绍了从互联网(站点)获取当前日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我想从Intrenet而不是在我的PC时间获取实时和日期怎么办?

例如.过去将此代码保存在记事本中并将其另存为html页面,现在可以运行此html页面.如果您将在线,那么您可以看到日期(伊朗).我们可以看到日期,但是我想在不加载任何网页的情况下在应用程序中获取日期.

 <   html      > 
<  头部 > 
    <  标题 > 无标题页面<  /title  > 
<  /head  > 
<  正文 > 
<   iframe     ="   http://free.timeanddate.com/clock/i33dnb3p/n246/tt1/tw0/tm3/td2"  frameborder    0" 宽度  ="    高度  ="  >  <  /iframe  <  /body  > 
<  /html  >  

解决方案

我有代码,仅在C#中,因为我没有安装VB.我希望这有帮助.有趣的问题,我今天学到了一些东西:)

控制台应用程序如下:

 静态  void  Main(字符串 []参数)
{
    // 伊朗的坐标
    字符串纬度= " ;
    字符串经度= " ;

    字符串 uri = 字符串 .Format(
       " ,纬度,经度);
    字符串 xml = PostRequest(uri, string  .Empty,MediaTypeNames.Text.Html);
    
    时区实例= Deserialize(xml);
    DateTime localTime = instance.GetLocalTime();

    Console.WriteLine(
       "  + localTime.ToString(" ));
    Console.Read();
}

私有 静态时区反序列化(字符串 xmlText)
{
    使用(MemoryStream memStream =
        MemoryStream(Encoding.UTF8.GetBytes(xmlText)))
    {
        XmlSerializer ser =  XmlSerializer( typeof (时区));
        返回(时区)ser.Deserialize(memStream);
    }
}

私有 静态 字符串 PostRequest(字符串 uri,字符串 msgBody,字符串 contentType)
{
    HttpWebRequest webReq =(HttpWebRequest)WebRequest.Create(uri);
    webReq.Timeout =  60  *  1000 ;
    webReq.Method = " ;
    webReq.ContentType = contentType;
    webReq.ContentLength = msgBody.Length;

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

    字符串 response = 使用(StreamReader streamIn =
        StreamReader(webReq.GetResponse().GetResponseStream()))
    {
        响应= streamIn.ReadToEnd();
        streamIn.Close();
    }

    返回响应;
} 



类时区的定义如下:

 [Serializable()]
公共  GpsCoordinates
{
    公共 字符串纬度{ get ; 设置; }
    公共 字符串经度{ get ; 设置; }
}

[Serializable()]
公共 时区
{
    公共 字符串版本{ get ; 设置; }
    公共 GpsCoordinates位置{ get ; 设置; }
    公共 字符串 offset { get ; 设置; }
    公共 字符串后缀{ get ; 设置; }
    公共 字符串本地时间{ get ; 设置; }
    公共 字符串 isotime { get ; 设置; }
    公共 字符串 utctime { get ; 设置; }
    公共 字符串 dst { get ; 设置; }

    公共 DateTime GetLocalTime()
    {
        返回 DateTime.Parse(localtime);
    }
} 



输出如下:

日期和时间是:2012年5月4日02:46:01


使用HttpWebRequest类下载网页"www.worldtimeserver.com/current_time_in_IR.aspx".然后,您可以从已下载的站点解析HTML文本,并搜索包含时间的HTML标记....

 <   div     id   ="  > 
   <   span     ="   font7" <  > 
<  /div  >  



:)


鉴于您尚未指定任何编程语言,我们只能猜测您要做什么.但是,您可以在提供 local 时间的调用时间和日期例程之前设置时区.即UTC时间已调整为特定时区.查看您使用的语言或API的相关时间方法.


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  >
<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>

解决方案

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:

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:

[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


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....

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



:)


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.


这篇关于从互联网(站点)获取当前日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆