Ehsan Akbar Ehsan Akbar - 1 year ago 56
C# Question

convert standard date to standard persian date in c#

I have a function that can convert the standard date to persian standard date as you can see here:

public string ConvertToPersianToShow(DateTime datetime)
PersianCalendar persian_date = new PersianCalendar();
string date;
string year = Convert.ToString(persian_date.GetYear(datetime));
string Month = Convert.ToString(persian_date.GetMonth(datetime));
string day = Convert.ToString(persian_date.GetDayOfMonth(datetime));

date = year+"/" + Month + "/" + day;
return date;

But i have a problem with these outputs:


These outputs should be like this :


I can count the digit numbers of the day and month but i thing it isn't the best way to do that .could you please give me some help about how can i change these kind of outputs using

Answer Source

All you need to do is add a leading zero. .ToString can be used.

string Month = persian_date.GetMonth(datetime).ToString("D2");
string day = persian_date.GetDayOfMonth(datetime).ToString("D2");