You must Sign In to post a response.
  • Category: .NET

    Date Issue:how to get proper date and time

    if any one changes the date of system then how to get the proper date
    in the window application(offline) , user may change system date before or after install software then if any enters data then that data entered date is wrong, how to handle this date issue.

    using : C#.NET, SQL
    Application Type: window application
  • #767020
    YOU CAN QUERY FOM SQL- SERVER DATE TIME THAT IS SERVER TIME
    SRI RAMA PHANI BHUSHAN KAMBHAMPATI

  • #767021
    Hi,

    when user change any format or any country time, try to use UTC datetime.

    Ex : SELECT DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), YOUR_DATE);

    Regards,
    Kalandiyur Subramanian Mohan
    www.mohanks.com

  • #767034
    Hi,
    You can use Globalization as follows:

    using System.Globalization;
    using System.Threading;

    private void Form1_Load(object sender, EventArgs e)
    {
    CultureInfo objCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
    objCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
    objCulture.DateTimeFormat.DateSeparator = "-";
    Thread.CurrentThread.CurrentCulture = objCulture;
    }

    So that you can set the desired date format for each client.

  • #767040
    If you have internet you can try to get the data time using some third part webservices
    Following is one of the example, http://nist.time.gov/actualtime.cgi?lzbc=siqm9b will give you the date time.

    DateTime dateTime = DateTime.MinValue;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi?lzbc=siqm9b");
    request.Method = "GET";
    request.ContentType = "application/x-www-form-urlencoded";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    if (response.StatusCode == HttpStatusCode.OK)
    {
    StreamReader stream = new StreamReader(response.GetResponseStream());
    string html = stream.ReadToEnd();
    string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
    double milliseconds = Convert.ToInt64(time) / 1000.0;
    dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();
    }

    By Nathan
    Direction is important than speed

  • #767055
    refer below code

    public static DateTime GetNistTime()
    {
    var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
    var response = myHttpWebRequest.GetResponse();
    string todaysDates = response.Headers["date"];
    DateTime dateTime= DateTime.ParseExact(todaysDates, "ddd, dd MMM yyyy HH:mm:ss 'GMT'",CultureInfo.InvariantCulture.DateTimeFormat,DateTimeStyles.AssumeUniversal);
    return dateTime;
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments