在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
获取北京时间(方法有很多,这里只讲其中之一)代码如下: #region GetBeijingTime ///<summary> /// 获取标准北京时间2 ///</summary> ///<returns></returns> public static DateTime GetBeijingTime() { //t0 = new Date().getTime(); //nyear = 2011; //nmonth = 7; //nday = 5; //nwday = 2; //nhrs = 17; //nmin = 12; //nsec = 12; DateTime dt; WebRequest wrt = null; WebResponse wrp = null; try { wrt = WebRequest.Create("http://www.beijing-time.org/time.asp"); wrp = wrt.GetResponse(); string html = string.Empty; using (Stream stream = wrp.GetResponseStream()) { using (StreamReader sr = new StreamReader(stream, Encoding.UTF8)) { html = sr.ReadToEnd(); } } string[] tempArray = html.Split(';'); for (int i = 0; i < tempArray.Length; i++) { tempArray[i] = tempArray[i].Replace("\r\n", ""); } string year = tempArray[1].Substring(tempArray[1].IndexOf("nyear=") + 6); string month = tempArray[2].Substring(tempArray[2].IndexOf("nmonth=") + 7); string day = tempArray[3].Substring(tempArray[3].IndexOf("nday=") + 5); string hour = tempArray[5].Substring(tempArray[5].IndexOf("nhrs=") + 5); string minite = tempArray[6].Substring(tempArray[6].IndexOf("nmin=") + 5); string second = tempArray[7].Substring(tempArray[7].IndexOf("nsec=") + 5); string time = String.Format("{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, minite, second); dt = DateTime.Parse(time);//year + "-" + month + "-" + day + "" + hour + ":" + minite + ":" + second } catch (WebException) { return DateTime.Now; 修改本地时间,代码如下: #region SetLocalTime [DllImport("Kernel32.dll")] private static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime); [StructLayout(LayoutKind.Sequential)] private struct SYSTEMTIME { public ushort wYear; public ushort wMonth; public ushort wDayOfWeek; public ushort wDay; public ushort wHour; public ushort wMinute; public ushort wSecond; public ushort wMilliseconds; } public static void SetSystemTime(DateTime date) { SYSTEMTIME lpTime = new SYSTEMTIME(); lpTime.wYear = Convert.ToUInt16(date.Year); lpTime.wMonth = Convert.ToUInt16(date.Month); lpTime.wDayOfWeek = Convert.ToUInt16(date.DayOfWeek); lpTime.wDay = Convert.ToUInt16(date.Day); DateTime time = date; lpTime.wHour = Convert.ToUInt16(time.Hour); lpTime.wMinute = Convert.ToUInt16(time.Minute); lpTime.wSecond = Convert.ToUInt16(time.Second); lpTime.wMilliseconds = Convert.ToUInt16(time.Millisecond); SetLocalTime(ref lpTime); } #endregion
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论