您的位置:首页 > 编程语言 > C#

C#获取标准北京时间

2015-08-29 14:45 876 查看
 ///

        /// 获取标准北京时间

        ///

        ///

        public static DateTime GetStandardTime()

        {

            try

            {

                System.Net.WebClient webclient = new System.Net.WebClient();

                string strhtml = webclient.DownloadString("http://open.baidu.com/special/time/");

                int timeindex = strhtml.IndexOf("window.baidu_time(") + 18;

                string strtime = strhtml.Substring(timeindex, 13);

                long longtime = Convert.ToInt64(strtime) / 1000;

                DateTime oritime = Convert.ToDateTime("1970-1-1 0:0:0");

                oritime = oritime.AddSeconds(longtime).AddHours(8);//8为时区

                return oritime;

            }

            catch (WebException webex)

            {

                throw new WebException(webex.Message);

                //return DateTime.Now;

            }

            catch (Exception ex)

            {

                throw new Exception(ex.Message);

                //return DateTime.Now;

            }

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: