C#獲取標準北京時間

 ///

        /// 獲取標準北京時間

        ///

        ///

        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;

            }

        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章