none
program date vs. computer date RRS feed

  • Question

  • Is there a way that when a user changes the date on the computer the  program should get the date that the computer now says but it should let the user know that the computer date is not in sync with the proper date. Basically is there a way that the program could know the proper date regardless of the computers date?

    Debra has a question

    Friday, March 18, 2016 3:20 PM

Answers

  • Hello,

    Try the following

    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.NoCacheNoStore); //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;
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, March 19, 2016 2:46 PM
    Moderator

All replies

  • Hello,

     Please clarify what exactly you are requesting in the manner of program/computer date?

    Are you looking for operating system file properties or program build properties with date values?

    Thanks :)

    Saturday, March 19, 2016 1:17 AM
  • Hello,

    Try the following

    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.NoCacheNoStore); //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;
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, March 19, 2016 2:46 PM
    Moderator
  • will this get me the date or the time or both?

    Debra has a question

    Tuesday, March 22, 2016 3:54 PM
  • will this get me the date or the time or both?

    Debra has a question


    The return value is a DateTime which represents an instant in time, typically expressed as a date and time of day so yes it does return date and time.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, March 22, 2016 5:38 PM
    Moderator
  • Thank you so much it's working nicely the only problem is that the time on the NIST website is 3 hours earlier than the computer time. We go by EST. Is there anything that can be done about this?

    Debra has a question

    Wednesday, March 30, 2016 3:40 PM
  • You can subtract three hours .AddHours(-3)

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, March 30, 2016 3:53 PM
    Moderator
  • Why is it getting the time 3 hours back?

    Debra has a question

    Wednesday, March 30, 2016 3:58 PM
  • Been in meetings, here is an idea but not fool proof, check the time zone and adjust as needed.

    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.NoCacheNoStore); //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();
        }
    
        ReadOnlyCollection<TimeZoneInfo> tz;
        tz = TimeZoneInfo.GetSystemTimeZones();
        TimeZone zone = TimeZone.CurrentTimeZone;
    
        if (zone.StandardName == "Eastern Standard Time")
        {
            dateTime = dateTime.AddHours(-3);
        }
    
        return dateTime;
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, March 30, 2016 8:18 PM
    Moderator
  • Is this website catered to a specific time zone?

    Debra has a question

    Friday, April 1, 2016 3:09 PM