/ Learn

How to get the Hijri Date in Windows 8.1 Accurately?

I was trying to get the Hijri date while developing a windows 8.1 app but it wasn’t an easy task to do so especially when you know that unlike the normal .Net Framework , you can’t access Hijri Calendar Class here although it is available due to MSDN documentation.

 

To save your time , at first I used a simple web service that provides Hijri Date as a text and parsed it. The nice thing is that this service is provided by Al-Azhar (Dar Al-Iftaa) so it is credible and accurate and wouldn’t stop after a short time.

 

I will share with you the code I wrote to get the date from this service

           ConnectionProfile connection = NetworkInformation.GetInternetConnectionProfile();
           bool internet = connection != null && connection.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
        if (internet)
        {
            HttpClient client = new HttpClient();
            string res = await client.GetStringAsync("http://www.dar-alifta.org/dateservice.aspx?LangID=1");
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(res);
 
            DateTimeFormatter df = new DateTimeFormatter("longdate", new string[] { "ar-sa" });
            var date = df.Format(DateTime.Now);
 
            return doc.GetElementbyId("lblDate").InnerText;
 
        }

To parse the HTML result , I used HTML Agility Library.

Although the previous solution would be accurate , it still needs internet to get the result which would result in a problem if the user’s device isn’t connected to internet all time. So here is another way to get the date if there is no internet connection
 

      var date = df.Format(DateTime.Now);
      return date.ToString();

Combining these two ways should satisfy your user who wants to be aware of the Hijri Date anytime.