This article describes the method of C# to implement the standard Beijing time from network synchronization. Share it for your reference. The specific analysis is as follows:
This C# code can obtain standard Beijing time from the website, and can allow the local server to synchronize the correct Beijing time in real time with a simple combination.
#region /// <summary> /// Obtain standard Beijing time/// /// </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 = ("/?user=flash"); = ; wrp = (); StreamReader sr = new StreamReader((),Encoding.UTF8); string html = (); (); (); int yearIndex = ("<year>") + 6; int monthIndex = ("<month>") + 7; int dayIndex = ("<day>") + 5; int hourIndex = ("<hour>") + 6; int miniteIndex = ("<minite>") + 8; int secondIndex = ("<second>") + 8; string year = (yearIndex, ("</year>") - yearIndex); string month = (monthIndex, ("</month>") - monthIndex); string day = (dayIndex, ("</day>") - dayIndex); string hour = (hourIndex, ("</hour>") - hourIndex); string minite = (miniteIndex, ("</minite>") - miniteIndex); string second = (secondIndex, ("</second>") - secondIndex); dt = (year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + second); } catch (WebException) { return ("2013-1-1"); } catch (Exception) { return ("2013-1-1"); } finally { if (wrp != null) (); if (wrt != null) (); } return dt; } #endregion
I hope this article will be helpful to everyone's C# programming.