您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關c#中怎么獲取北京時間,文章內(nèi)容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
#region /// <summary> /// 獲取標準北京時間 /// /// </summary> /// /// <returns></returns> /// public static DateTime GetStandardTime() { /// //<?xml version="1.0" encoding="GB2312" ?> //- <ntsc> //- <time> // <year>2013</year> // <month>8</month> // <day>29</day> // <Weekday /> // <hour>16</hour> // <minite>29</minite> // <second>12</second> // <Millisecond /> // </time> // </ntsc> DateTime dt; WebRequest wrt = null; WebResponse wrp = null; try { wrt = WebRequest.Create("http://www.time.ac.cn/timeflash.asp?user=flash"); wrt.Credentials = CredentialCache.DefaultCredentials; wrp = wrt.GetResponse(); StreamReader sr = new StreamReader(wrp.GetResponseStream(),Encoding.UTF8); string html = sr.ReadToEnd(); sr.Close(); wrp.Close(); int yearIndex = html.IndexOf("<year>") + 6; int monthIndex = html.IndexOf("<month>") + 7; int dayIndex = html.IndexOf("<day>") + 5; int hourIndex = html.IndexOf("<hour>") + 6; int miniteIndex = html.IndexOf("<minite>") + 8; int secondIndex = html.IndexOf("<second>") + 8; string year = html.Substring(yearIndex, html.IndexOf("</year>") - yearIndex); string month = html.Substring(monthIndex, html.IndexOf("</month>") - monthIndex); string day = html.Substring(dayIndex, html.IndexOf("</day>") - dayIndex); string hour = html.Substring(hourIndex, html.IndexOf("</hour>") - hourIndex); string minite = html.Substring(miniteIndex, html.IndexOf("</minite>") - miniteIndex); string second = html.Substring(secondIndex, html.IndexOf("</second>") - secondIndex); dt = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + second); } catch (WebException) { return DateTime.Parse("2013-1-1"); } catch (Exception) { return DateTime.Parse("2013-1-1"); } finally { if (wrp != null) wrp.Close(); if (wrt != null) wrt.Abort(); } return dt; } #endregion
關于c#中怎么獲取北京時間就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。