0
چگونگی بدست آوردن تاریخ دقیق از طریق اینترنت در c#
باسلام و احترام ، دوستان عزیز به چه روشی میشه تاریخ دقیق را ازطریق اینترنت دریافت کرد و در c#از اون استفاده کرد. آیا تابع خاصی برای این منظور وجود دارد؟ یا حتی یک روش درست تر و ساده تر! با تشکر
2 پاسخ
0
Additional information: The request was aborted: Could not create SSL/TLS secure channel.
این خطا رو میده
2
using System.Net;
using System.Net.Cache;
using System.IO;
using System.Text.RegularExpressions;
public static DateTime GetNistTime()
{
DateTime dateTime = DateTime.MinValue;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi?lzbc=siqm9b");
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
request.ContentType = "application/x-www-form-urlencoded";
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStor e); //No caching
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader stream = new StreamReader(response.GetResponseStream());
string html = stream.ReadToEnd();//<timestamp time=\"1395772696469995\" delay=\"1395772696469995\"/>
string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
double milliseconds = Convert.ToInt64(time) / 1000.0;
dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();
}
return dateTime;
}
البته شما میتونید توسط تابع Time و Date زمان و تاریخ سیستم خودتونو دریافت کنید مثلاً یک lable داشته باشید و روی این lable توسط کد زیر از سیستم دریافت و نشون بدید
label1.Text = System.DateTime.Now.ToString()
ویا نمونه کد زیر:
using System;
public class Program
{
public static void Main()
{
DateTime myDate = DateTime.Now;
Console.WriteLine("Year: " + myDate.Year);
Console.WriteLine("Month: " + myDate.Month);
Console.WriteLine("Day: " + myDate.Day);
Console.WriteLine("Today is {0}.", myDate.DayOfWeek.ToString());
//Assign newDate with the current date added by 3 days
DateTime newDate = myDate.AddDays(3);
Console.WriteLine("The date 3 days from now is {0}.",
newDate.ToShortDateString());
//Assign newdate with the current date subtracted by 3 days
newDate = myDate.AddDays(-3);
Console.WriteLine("The date 3 days ago is {0}.", newDate.ToShortDateString());
}
}
که خروجی آن به صورت زیر است:
Year: 2010
Month: 10
Day: 3
Today is Sunday.
The date 3 days from now is 1062010.
The date 3 days ago is 9302010.