怎么获取time.nist.gov服务器的系统啊
解决方案 »
- 运算符“&”无法应用于“bool”和“string”类型的操作数
- C# 控制IE浏览器打印问题。
- c#桌面应用程序到底该怎么发布?
- 从数据库读取图片出错,帮帮忙(sqlserver2000)
- 怎么检查数据表中是否有重复数据存在
- 如何把datareader中得知取出放到label或textbox中,我知道VB的,但不知道C#语法是什么
- 我想给程序来点配置信息,请问使用config还是xml?哪种效果好,实用方便?
- 求企业绩效考核程序!也请各位网友提些建议!
- 怎样获得两个DateTimePicker的天数差?并转换为int型
- 又有一个郁闷的问题
- webForm 中如何让ListBox 显示水平滚动条?
- 帮忙看看更新数据库的简单问题!
using System.Net;
using System.IO;
using System.Text.RegularExpressions; 然后送给你两个函数,一个获得直接nist时间和日期,一个获得对应的中国时间和日期
'获得nist时间和日期
private void GetNistTimeUS( out string time, out string Data )
{
string strNistUrl = "http://nist.time.gov/timezone.cgi?UTC/s/0"; System.Net.WebRequest myHttpWebRequest = System.Net.HttpWebRequest.Create( strNistUrl );
System.Net.WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse(); Stream sr = myHttpWebResponse.GetResponseStream();
StreamReader reader = new StreamReader(sr, System.Text.Encoding.ASCII);
String srdata = reader.ReadToEnd();
srdata = srdata.Replace( "\"","\'"); string strOut = Regex.Split( srdata, "color='white'><b>" , RegexOptions.IgnoreCase)[1];
string strTime = Regex.Split( strOut , "<br>", RegexOptions.IgnoreCase )[0];
time = DateTime.Parse( strTime ).ToLongTimeString(); string strOut1 = Regex.Split( srdata, "'5' color='white'>" , RegexOptions.IgnoreCase)[1];
string strData = Regex.Split( strOut1 , "<br>", RegexOptions.IgnoreCase )[0];
Data = DateTime.Parse( strData ).ToShortDateString();
}'获得中国时间和日期
private void GetNistTimeCN( out string time, out string Data )
{
string strNistUrl = "http://nist.time.gov/timezone.cgi?UTC/s/0"; System.Net.WebRequest myHttpWebRequest = System.Net.HttpWebRequest.Create( strNistUrl );
System.Net.WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse(); Stream sr = myHttpWebResponse.GetResponseStream();
StreamReader reader = new StreamReader(sr, System.Text.Encoding.ASCII);
String srdata = reader.ReadToEnd();
srdata = srdata.Replace( "\"","\'"); string strOut = Regex.Split( srdata, "color='white'><b>" , RegexOptions.IgnoreCase)[1];
string strTime = Regex.Split( strOut , "<br>", RegexOptions.IgnoreCase )[0];
time = DateTime.Parse( strTime ).AddHours( 8 ).ToLongTimeString(); string strOut1 = Regex.Split( srdata, "'5' color='white'>" , RegexOptions.IgnoreCase)[1];
string strData = Regex.Split( strOut1 , "<br>", RegexOptions.IgnoreCase )[0];
int intHourCn = int.Parse( time.Split( ':' )[0] );
int intHourUS = int.Parse( DateTime.Parse( strTime ).ToLongTimeString().Split( ':' )[0] ); if ( intHourCn < intHourUS )
{
Data = DateTime.Parse( strData ).AddDays(1).ToShortDateString();
}
else
{
Data = DateTime.Parse( strData ).ToShortDateString();
}
}
public static extern bool SetSystemTime(ref SystemTime sysTime); [StructLayout(LayoutKind.Sequential)]
public 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 wMiliseconds;
} /// <summary>
/// 设置系统时间
/// </summary>
/// <param name="sDate">2006-5-20</param>
/// <param name="sTime">20:32:20</param>
public static void setSystemTime(string sDate, string sTime)
{
int iTmpTimeVal = 0; string sYYYY = string.Empty;
string sMM = string.Empty;
string sDD = string.Empty;
string sHH = string.Empty;
string sMinute = string.Empty;
string sSecond = string.Empty; sYYYY = sDate.Substring(0, sDate.IndexOf("-"));
sDate = sDate.Substring(sDate.IndexOf("-") + 1); sMM = sDate.Substring(0, sDate.IndexOf("-"));
sDate = sDate.Substring(sDate.IndexOf("-") + 1); sDD = sDate; sHH = sTime.Substring(0, sTime.IndexOf(":"));
sTime = sTime.Substring(sTime.IndexOf(":") + 1); sMinute = sTime.Substring(0, sTime.IndexOf(":"));
sTime = sTime.Substring(sTime.IndexOf(":") + 1); sSecond = sTime; SystemTime systemTime = new SystemTime();
systemTime.wYear = Convert.ToUInt16(sYYYY);
systemTime.wMonth = Convert.ToUInt16(sMM);
systemTime.wDay = Convert.ToUInt16(sDD);
systemTime.wHour = Convert.ToUInt16(sHH);
systemTime.wMinute = Convert.ToUInt16(sMinute);
systemTime.wSecond = Convert.ToUInt16(sSecond); if (SetSystemTime(ref systemTime))
{ }
}
/// 获得nist时间和日期
/// </summary>
/// <param name="time"></param>
/// <param name="Data"></param>
public static DateTime? GetNistTimeUS()
{
DateTime? dt=null;
try
{
string strNistUrl = "http://nist.time.gov/timezone.cgi?UTC/s/0"; System.Net.WebRequest myHttpWebRequest = System.Net.HttpWebRequest.Create(strNistUrl);
System.Net.WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse(); Stream sr = myHttpWebResponse.GetResponseStream();
StreamReader reader = new StreamReader(sr, System.Text.Encoding.ASCII);
String srdata = reader.ReadToEnd(); srdata = srdata.Replace("\"", "\'"); string strOut = Regex.Split(srdata, "color='white'><b>", RegexOptions.IgnoreCase)[1];
string strTime = Regex.Split(strOut, "<br>", RegexOptions.IgnoreCase)[0];
string strOut1 = Regex.Split(srdata, "'5' color='white'>", RegexOptions.IgnoreCase)[1];
string strData = Regex.Split(strOut1, "<br>", RegexOptions.IgnoreCase)[0]; dt = DateTime.Parse(strData + " " + strTime);
}
catch
{
}
return dt;
}