C# 获取北京时间,并修改本地时间与北京时间同步

获取北京时间(方法有很多,这里只讲其中之一)代码如下:

     #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;
} catch (Exception) { return DateTime.Now; } finally { if (wrp != null) wrp.Close(); if (wrt != null) wrt.Abort(); } return dt; } #endregion

修改本地时间,代码如下:

 #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

 

 

posted @ 2013-12-06 14:06  JasonGu0  阅读(2529)  评论(0编辑  收藏  举报