• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

     #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

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#通过SendMessage获取浏览器地址栏的地址发布时间:2022-07-10
下一篇:
C#SQL多条件查询拼接技巧发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap