0

I live in Israel so the timezone should be for Jerusalem or other city in Israel. If i'm searching in google for: israel time i'm getting: 5:22 But with the code it return 30/11/2016 19:23:05 so the date and the time are wrong. It's now here 5:22 in the morning 01/12/2016

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Net.Cache;

namespace DateTime
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();

            System.DateTime dt = GetNistTime();
        }

        public static System.DateTime GetNistTime()
        {
            System.DateTime dateTime = System.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 System.DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();
            }

            return dateTime;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
benny dayag
  • 139
  • 9

2 Answers2

1

Look this how can get DateTime From Internet (External Resource - Not From Server) .The answer gives correct date and time for my time zone GMT+4. But i tried the solution only with one server instead of trying to get time from 5 random.

Community
  • 1
  • 1
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
0

Examples The following example demonstrates the ToLocalTime method. Note that the exact output depends on the current culture and the local time zone of the system on which it is run.

using System;
class Example
{
    static void Main()
    {
        DateTime localDateTime, univDateTime;
        Console.WriteLine("Enter a date and time.");
        string strDateTime = Console.ReadLine();
        try {
            localDateTime = DateTime.Parse(strDateTime);
            univDateTime = localDateTime.ToUniversalTime();
            Console.WriteLine("{0} local time is {1} universal time.",localDateTime,univDateTime); 
        }
        catch (FormatException) {
            Console.WriteLine("Invalid format.");
            return;
        }
        Console.WriteLine("Enter a date and time in universal time.");
        strDateTime = Console.ReadLine();
        try {
            univDateTime = DateTime.Parse(strDateTime);
            localDateTime = univDateTime.ToLocalTime();
            Console.WriteLine("{0} universal time is {1} local time.", univDateTime,localDateTime); 
        }
        catch (FormatException) {
            Console.WriteLine("Invalid format.");
            return;
        }
    }
}

The example displays output like the following when run on a computer whose culture is en-US in the Pacific Standard Time zone: Enter a date and time. 12/10/2015 6:18 AM 12/10/2015 6:18:00 AM local time is 12/10/2015 2:18:00 PM universal time. Enter a date and time in universal time. 12/20/2015 6:42:00 12/20/2015 6:42:00 AM universal time is 12/19/2015 10:42:00 PM local time.

reference : https://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime(v=vs.110).aspx

BehrouzMoslem
  • 9,053
  • 3
  • 27
  • 34