Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.30/5 (3 votes)
See more:
hi
i'm sorry for my poor english
i want to convert now date to hijri format and get it in string format how?
please write code for me.
Thanks.
Posted
Updated 20-Mar-19 6:57am
v2

Something tells me that Hijri calendar, or "Islamic calendar" might be not supported by .NET in its standard way, through ToString(IFormatProvider) or System.DateTime.ToString(String, IFormatProvider), if you used the implementation of the interface System.IFormatProvider as it is implemented by the type System.Globalization.CultureInfo, as it is usually done.

Please see:
http://msdn.microsoft.com/en-us/library/ht77y576.aspx[^],
http://msdn.microsoft.com/en-us/library/8tfzyc64.aspx[^],
http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx[^].

I think, at least one background reason of such state of affairs is that the format providers based on particular cultures, are based on cultural preferences related to certain nations, or group of nations sharing some cultural elements such as language, but Islamic cultural traditions are pretty much international and only partially integrate in national cultures. You can take a chance of trying particular cultures of some Muslim countries, but I don't think that Hijri dominates even there to the extent that it can be included in the standard. You could just try though:
http://en.wikipedia.org/wiki/Hijri[^],
http://en.wikipedia.org/wiki/List_of_Muslim-majority_countries[^].

The code sample for such approach is shown in a MSDN article I referenced above, very first link; you can quickly try all country-based Islamic cultures, but I don't think you will find the case formatting as Hijri. So, even though I'm to best of my knowledge, you won't success in using just .NET Base Class Library (BCL), so you will need to apply fully custom approach.

First of all, you can develop a Hijri-specific of the System.IFormatProvider by yourself. In a more ad-hoc approach, you can simply develop your own variant of System.DateTime.ToString which could be as simple as having something like string TimeToHijriString(System.DateTime time /* , some options? */). All you would need is the description of the calendar I referenced above, plus using individual fields of System.DateTime.ToString for year, month, etc.:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

As the Hijri also has 12 months per year, 7 days a week, I hope you can map it to the Gregorian calendar (known to all computer systems, http://en.wikipedia.org/wiki/Gregorian_calendar[^]) pretty well, without big problems.

Wish you the best of luck,
—SA
 
Share this answer
 
v2
Comments
fjdiewornncalwe 17-Oct-12 17:12pm    
+5.
Sergey Alexandrovich Kryukov 17-Oct-12 17:14pm    
Thank you, Marcus.
--SA
Behnam Mohammadi 18-Oct-12 3:10am    
Thanks.
Sergey Alexandrovich Kryukov 18-Oct-12 13:51pm    
You are welcome.
Will you accept the answer formally (green button) -- thanks.
--SA
Hi,

Use this Method to convert from Hijri to Gregorian

C#
public static string ConvertDateCalendar(DateTime DateConv, string Calendar, string DateLangCulture)
      {
          DateTimeFormatInfo DTFormat;
          DateLangCulture = DateLangCulture.ToLower();
          /// We can't have the hijri date writen in English. We will get a runtime error

          if (Calendar == "Hijri" && DateLangCulture.StartsWith("en-"))
          {
              DateLangCulture = "ar-sa";
          }

          /// Set the date time format to the given culture
          DTFormat = new System.Globalization.CultureInfo(DateLangCulture, false).DateTimeFormat;

          /// Set the calendar property of the date time format to the given calendar
          switch (Calendar)
          {
              case "Hijri":
                  DTFormat.Calendar = new System.Globalization.HijriCalendar();
                  break;

              case "Gregorian":
                  DTFormat.Calendar = new System.Globalization.GregorianCalendar();
                  break;

              default:
                  return "";
          }

          /// We format the date structure to whatever we want
          DTFormat.ShortDatePattern = "dd/MM/yyyy";
          return (DateConv.Date.ToString("f", DTFormat));
      }


How to use

C#
string date = ConvertDateCalendar(DateTime.Now, "Hijri", "en-US")


Hope this may help you
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Oct-12 23:59pm    
From the point of view of maintenance and elementary programming culture, using string parameter and a switch ("Hijri", "Gregorian") is not just bad code: it's a real crime (I guess, against yourself, but do you wish OP the same?) and a sign that you have no a clue how to do programming, no matter how many applications you created. After this piece of code, no need to read the rest of it.

If you did not offer it as a "solution" and tried to teach others, it would be explainable, but for a "solution"...

Please understand it: I cannot vote any higher then 1, sorry about it.
--SA
Behnam Mohammadi 18-Oct-12 3:09am    
Thanks.
Jabir Puthusseri 24-Apr-14 15:13pm    
it converts the system date to hijri date ,but i want to convert hijri date to Gregorian
please find a solution for this
STEEF-R 22-Mar-18 9:31am    
thank u
put i have a quststion
i try string date = ConvertDateCalendar(DateTime.Now, "Gregorian", "en-US")
put date is still hijri not convert to Gregorian

please help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900