C# 时间更新器
private void button1_Click(object sender, EventArgs e)
{
//DateTime dt1 = new DateTime(1594363972686);
DateTime dt = this.GetTaoBaoTimeAPI();
bool falg = UpdateTime.SetDate(dt);
}
private void button2_Click(object sender, EventArgs e)
{
DateTime dt = this.GetbeijingAPI();
bool falg = UpdateTime.SetDate(dt);
}
private void button3_Click(object sender, EventArgs e)
{
DateTime dt = this.GetSuningTimeAPI();
bool falg = UpdateTime.SetDate(dt);
}
/// <summary>
/// 淘宝
/// </summary>
/// <returns></returns>
private DateTime GetTaoBaoTimeAPI()
{
string url = "http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp";
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
hwr.Method = "get";
hwr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
hwr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.3 Safari/537.36";
DateTime dt;
try
{
HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
string webtime = new StreamReader(hws.GetResponseStream()).ReadToEnd();
long times = long.Parse(Regex.Match(webtime, "\\d{13}").Value);
//TimeSpan timeSpan = new TimeSpan(times);
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
dt = startTime.AddMilliseconds(times);
}
catch (Exception ex)
{
throw ex;
}
return dt;
}
/// <summary>
/// beijing-time
/// </summary>
/// <returns></returns>
private DateTime GetbeijingAPI()
{
string url = "http://time1909.beijing-time.org/time.asp";
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
hwr.Method = "get";
hwr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
hwr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.3 Safari/537.36";
DateTime dt;
try
{
HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
string webtime = new StreamReader(hws.GetResponseStream()).ReadToEnd();
string[] dtstrs = webtime.Split(';');
string nyear = string.Empty, nmonth = string.Empty, nday = string.Empty, nhrs = string.Empty, nmin = string.Empty, nsec = string.Empty;
foreach (string item in dtstrs)
{
if (item.Contains("nyear"))
{
nyear = Regex.Match(item.Replace("\r\n", ""), "[\\d]+", RegexOptions.Multiline).Value;
}
else if (item.Contains("nmonth"))
{
nmonth = Regex.Match(item, "[\\d]+", RegexOptions.Multiline).Value;
}
else if (item.Contains("nday"))
{
nday = Regex.Match(item, "[\\d]+", RegexOptions.Multiline).Value;
}
else if (item.Contains("nhrs"))
{
nhrs = Regex.Match(item, "[\\d]+", RegexOptions.Multiline).Value;
}
else if (item.Contains("nmin"))
{
nmin = Regex.Match(item, "[\\d]+", RegexOptions.Multiline).Value;
}
else if (item.Contains("nsec"))
{
nsec = Regex.Match(item, "[\\d]+", RegexOptions.Multiline).Value;
}
}
dt = new DateTime(int.Parse(nyear), int.Parse(nmonth), int.Parse(nday), int.Parse(nhrs), int.Parse(nmin), int.Parse(nsec));
}
catch (Exception ex)
{
throw ex;
}
return dt;
}
/// <summary>
/// 苏宁
/// </summary>
/// <returns></returns>
private DateTime GetSuningTimeAPI()
{
string url = "http://quan.suning.com/getSysTime.do";
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
hwr.Method = "get";
hwr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
hwr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.3 Safari/537.36";
DateTime dt;
try
{
HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
string webtime = new StreamReader(hws.GetResponseStream()).ReadToEnd();
long times = long.Parse(Regex.Match(webtime, "\\d{14}").Value);
int y = int.Parse(times.ToString().Substring(0, 4));
int mm = int.Parse(times.ToString().Substring(4, 2));
int d = int.Parse(times.ToString().Substring(6, 2));
int h = int.Parse(times.ToString().Substring(8, 2));
int m = int.Parse(times.ToString().Substring(10, 2));
int s = int.Parse(times.ToString().Substring(12, 2));
dt = new DateTime(y,mm,d,h,m,s);
}
catch (Exception ex)
{
throw ex;
}
return dt;
}
https://time.is/t/?zh.0.347.1989.0p.480.43d.1594451166998.1594451162678