Search code examples
c#datetimecurrentuiculturepersian-calendar

Return Datetime.Day in persian culture by c#


I intend to return the day of the month in Persian culture. I have already set the culture and it works well. the day which shows is in Persian calendar. But I need to check somewhere the exact day of the month,

for example today is 2018-2-3 and in Persian calendar is 1396-11-14,

var date = DateTime.Now 

shows me the right Datetime in Persian calendar. But

var date = DateTime.Now.Day 

shows me 3 rather than 14. I need 14! I know I can change it to string and get the day but it make my code so dirty and I am not so interested in this way, because I used this in many parts of the program. Is there any way ?


Solution

  • I want to clarify a few things.

    A DateTime instance is always on Gregorian calendar no matter what. Suprisingly, it has a few constructors that takes Calendar as a parameter but I don't think this parameter effect anything about keeping it as a Gregorian equivalent internally.

    If you wanna get some parts of a DateTime in a specific calendar, you need to call that DateTime instance as a parameter of GetXXX methods on that calendar instance.

    For examle;

    var today = DateTime.Today;
    var persian = new PersianCalendar();
    Console.WriteLine(persian.GetDayOfMonth(today)); // 14
    

    Here a demonstration.