Search code examples
c#datehijri

Getting Gregorian date not Hijri date in windows 10


I use Hijri calendar on my windows 10. I'm working on a C# program and I tried using of DateTime.Now to get AD date but it gave me Hijri date.

So, how could I get AD date, not hijri date?

Must I convert Hijri to AD date?


Solution

  • DateTime itself is always Gregorian - DateTime.Year etc will always return the value in the Gregorian calendar.

    However, the ToString method will take into account the default calendar of the culture it's using to format. That culture is (by default) the current thread culture.

    The right answer here depends on whether you want to use the current thread culture for other aspects of formatting.

    If you want culture-sensitive formatting (because you're displaying this directly to a user), you could clone the culture and set the DateTimeFormat.Calendar in the clone to a GregorianCalendar, then pass the clone to the ToString method.

    If you actually want to use culture-invariant formatting (because this is intended to be machine-readable, e.g. in a JSON document) just pass CultureInfo.InvariantCulture to ToString.

    (If you're actually worried about how it's formatted in the debugger, I'm not sure of the best answer...)