這篇文章將為大家詳細講解有關(guān)c#中怎么獲取北京時間,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設(shè)計、網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)蟠龍,十載網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
#region ////// 獲取標(biāo)準(zhǔn)北京時間 /// /// /// ////// public static DateTime GetStandardTime() { /// // //- //- // 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("") + 6; int monthIndex = html.IndexOf(" ") - yearIndex); string month = html.Substring(monthIndex, html.IndexOf("") - monthIndex); string day = html.Substring(dayIndex, html.IndexOf("") - dayIndex); string hour = html.Substring(hourIndex, html.IndexOf("") - hourIndex); string minite = html.Substring(miniteIndex, html.IndexOf("") - miniteIndex); string second = html.Substring(secondIndex, html.IndexOf("") - 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") + 7; int dayIndex = html.IndexOf(" ") + 5; int hourIndex = html.IndexOf(" ") + 6; int miniteIndex = html.IndexOf(" ") + 8; int secondIndex = html.IndexOf(" ") + 8; string year = html.Substring(yearIndex, html.IndexOf("
關(guān)于c#中怎么獲取北京時間就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。