16
2012
04

C#如何获取北京时间

C#获取北京时间,也同样使用Web获取时间,通过Web获取到到北京时间,再转换为C#中的时间
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LoversQQ
{
public partial class Form1 : Form
{
private int index = 0;
private string time = "0";
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Refresh(WebBrowserRefreshOption.Completely);
}

private void timer1_Tick(object sender, EventArgs e)
{
updateTime();
//getTime(index);
}

private string getTime(int _index)
{
int int1 = -1, int2 = -1, int3 = -1;
//MessageBox.Show(int1.ToString()+Environment.NewLine+int2.ToString()+Environment.NewLine+int3.ToString());
try
{
string str = webBrowser1.Document.All[_index].InnerText;
if (str == null || str == string.Empty)
return "0";
if ((int1 = str.IndexOf("服务器时间:")) != -1 || (int1 = str.IndexOf("服务器时间:")) != -1)
{
int2 = str.IndexOf("客户机时间");
int3 = str.IndexOf("如需精准对时请进入");
if (int1 == -1 || int2 == -1 || int3 == -1)
{
return "0";
}
string mystr = str.Substring(int1 + 6, int2 - int1 - 6).Trim();
//mystr += Environment.NewLine + str.Substring(int2+6, int3 - int2-6).Trim();
if (mystr.Length < 21 && mystr.Length > 16)
{
string[] strs = mystr.Split(new char[] { '年', '月', '日', '号', ':', ':' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string st in strs)
{
//MessageBox.Show(st);
}
//MessageBox.Show(mystr + Environment.NewLine + mystr.Length.ToString());
//label2.Text = mystr;
return mystr;
}
}
}
catch
{
return "0";
}
return "0";
}

private bool changeIndex()
{
try
{
for (int i = 0; i < webBrowser1.Document.All.Count; i++)
{
if (getTime(i) != "0")
{
index = i;
return true;
}
}
return true;
}
catch { }
return false;
}

private void updateTime()
{
time=getTime(index);
if (time!= "0")
label2.Text =time;
else
changeIndex();
}

private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
}
}
« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。