日期:2014-05-17  浏览次数:20797 次

请问,winform如何显示官网北京时间
请问,winform的Label,如何显示官网北京时间,而不是本地电脑时间

------解决方案--------------------
#region 获取网络时间

///<summary>
/// 获取中国国家授时中心网络服务器时间发布的当前时间
///</summary>
///<returns></returns>
public static DateTime GetChineseDateTime()
{
    DateTime res = DateTime.MinValue;
    try
    {
        string url = "http://www.time.ac.cn/stime.asp";
        HttpHelper helper = new HttpHelper();
        helper.Encoding = Encoding.Default;
        string html = helper.GetHtml(url);
        string patDt = @"\d{4}年\d{1,2}月\d{1,2}日";
        string patHr = @"hrs\s+=\s+\d{1,2}";
        string patMn = @"min\s+=\s+\d{1,2}";
        string patSc = @"sec\s+=\s+\d{1,2}";
        Regex regDt = new Regex(patDt);
        Regex regHr = new Regex(patHr);
        Regex regMn = new Regex(patMn);
        Regex regSc = new Regex(patSc);

        res = DateTime.Parse(regDt.Match(html).Value);
        int hr = GetInt(regHr.Match(html).Value, false);
        int mn = GetInt(regMn.Match(html).Value, false);
        int sc = GetInt(regSc.Match(html).Value, false);
        res = res.AddHours(hr).AddMinutes(mn).AddSeconds(sc);
    }
    catch { }
    return res;
}

///<summary>
/// 从指定的字符串中获取整数
///</summary>
///<param name="origin">原始的字符串</param>
///<param name="fullMatch">是否完全匹配,若为false,则返回字符串中的第一个整数数字</param>
///<returns>整数数字</returns>
private static int GetInt(string origin, bool fullMatch)
{
    if (string.IsNullOrEmpty(origin))
    {
        return0;
    }
    origin = origin.Trim();
    if (!fullMatch)
    {
        string pat = @"-?\d+";
        Regex reg = new Regex(pat);
        origin = reg.Match(origin.Trim()).Value;
    }
    int res = 0;
    int.TryParse(origin, out res);
    return res;
}

#endregion


///<summary>
/// 获取标准北京时间1
///</summary>
///<returns></r