29

What is the simplest way to print c# DateTime in Persian? currently I'm using :

static public string PersianDateString(DateTime d)
{
    CultureInfo faIR = new CultureInfo("fa-IR");
    faIR.DateTimeFormat.Calendar = new PersianCalendar();            
    return d.ToString("yyyy/MM/dd", faIR);
}

Which throws an exception

Not a valid calendar for the given culture

shA.t
  • 16,580
  • 5
  • 54
  • 111
user590747
  • 345
  • 1
  • 4
  • 9

5 Answers5

40

First you must note that you cannot put a Jalali Date in a DateTime object, you will get an exception in dates like "1392/02/31".

So you must handle your Jalali dates in a string or a custom DateTime type.

I suggest that you keep your date as a Gregorian date at all time, and convert it when you need to show it.

This is an extension method to get Persian Date from a DateTime object.

    public static string GetPersianDate(this DateTime date)
    {
        PersianCalendar jc = new PersianCalendar();
        return string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), jc.GetMonth(date), jc.GetDayOfMonth(date));
    }
    //How to use it:
    DateTime d = new DateTime(2014, 05, 21);
    string s = d.GetPersianDate(); //1393/02/31

And then when you have a Jalali date in string format here is the extension method to get the Gregorian date:

    public static DateTime GetDateTimeFromJalaliString(this string jalaliDate)
    {
        PersianCalendar jc = new PersianCalendar();

        try
        {
            string[] date = jalaliDate.Split('/');
            int year = Convert.ToInt32(date[0]);
            int month = Convert.ToInt32(date[1]);
            int day = Convert.ToInt32(date[2]);

            DateTime d = jc.ToDateTime(year, month, day, 0, 0, 0, 0, PersianCalendar.PersianEra);

            return d;
        }
        catch
        {
            throw new FormatException("The input string must be in 0000/00/00 format.");
        }
    }
    //How to use it:
    string pdate = "1392/02/31";
    DateTime dateFromJalali = pdate.GetDateTimeFromJalaliString(); //{5/21/2014 12:00:00 AM}

And now to handle extra functions:

Week name:

    public static string GetDayOfWeekName(this DateTime date)
    {
        switch (date.DayOfWeek)
        {
            case DayOfWeek.Saturday: return "شنبه";
            case DayOfWeek.Sunday: return "يکشنبه";
            case DayOfWeek.Monday: return "دوشنبه";
            case DayOfWeek.Tuesday: return "سه‏ شنبه";
            case DayOfWeek.Wednesday: return "چهارشنبه";
            case DayOfWeek.Thursday: return "پنجشنبه";
            case DayOfWeek.Friday: return "جمعه";
            default: return "";
        }
    }
    //How to use it:
    DateTime date = DateTime.Now;
    string wname = date.GetDayOfWeekName();

Month name:

    public static string GetMonthName(this DateTime date)
    {
            PersianCalendar jc = new PersianCalendar();
            string pdate = string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), jc.GetMonth(date), jc.GetDayOfMonth(date));

            string[] dates = pdate.Split('/');
            int month = Convert.ToInt32(dates[1]);

            switch (month)
            {
                case 1: return "فررودين";
                case 2: return "ارديبهشت";
                case 3: return "خرداد";
                case 4: return "تير‏";
                case 5: return "مرداد";
                case 6: return "شهريور";
                case 7: return "مهر";
                case 8: return "آبان";
                case 9: return "آذر";
                case 10: return "دي";
                case 11: return "بهمن";
                case 12: return "اسفند";
                default: return "";
            }

    }
    //How to use it:
    DateTime date = DateTime.Now;
    string mname = date.GetMonthName();
Ashkan Ghodrat
  • 3,162
  • 2
  • 32
  • 36
13

Try like this:

public static string PersianDateString(DateTime d)
{
    CultureInfo faIR = new CultureInfo("fa-IR");
    return d.ToString("yyyy/MM/dd", faIR);
}

and if you changed the format to for example "yyyy MMM ddd" you would get 2011 جوييه الأحد.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    The most important things is it's can't print Persian weekday and month names and just print Arabic names. Persian month names are فروردین - اردیبهشت - خرداد etc and Persian weekday names are شنبه - یکشنبه etc. This is completely usefulness. – QMaster Feb 27 '14 at 16:44
  • I tested this code on my machine and worked for Persian date, great answer. – Mehdi Dehghani Jun 14 '17 at 10:43
  • If you don't use the PersianCalendar class then you can get ArgumentOutofRange on same dates where the number of days are different in the month – ickydime Oct 07 '21 at 13:08
2

You can use this:

static public string PersianDateString(DateTime d)
{
    var pc = new PersianCalendar();
    return string.Format("{0:0000}/{1:00}/{2:00}",
        pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d));
}
Kamyar Infinity
  • 2,711
  • 1
  • 21
  • 32
  • 1
    That is just convert date to Persian. You must get useful points. The question about formatting as Persian date. like this: چهارشنبه . پنجم آذر ماه 1392 and your answer is usefulness. – QMaster Feb 27 '14 at 16:52
1

I create this static class The result will be displayed in this format سه شنبه 28 اسفند 1398

public static class PersianDateString
{
    private static string[] Days = { "یک شنبه", "دو شنبه", "سه شنبه", "چهار شنبه", "پنج شنبه", "جمعه", "شنبه" };
    private static string[] Months = { "فروردین", "اریبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن" , "اسفند" };
    private static PersianCalendar pc = new PersianCalendar();

    public static string ToPersianDateString(this DateTime date)
    {
        return ($"{Days[pc.GetDayOfWeek(date).GetHashCode()]} {pc.GetDayOfMonth(date)} {Months[pc.GetMonth(date) - 1]} {pc.GetYear(date)}");
    }

}

Usage

string ptime = DateTime.Now.ToPersianDateString();
Shervin Ivari
  • 1,759
  • 5
  • 20
  • 28
1

This seams works too:

DateTime d= DateTime.Now;
Console.WriteLine( d.ToString("yyyy/MM/dd", new CultureInfo("fa-IR")) );

Outbut:

1399/06/26
MSS
  • 3,520
  • 24
  • 29