29

用波斯语打印 c# DateTime 的最简单方法是什么?目前我正在使用:

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

抛出异常

不是给定文化的有效日历

4

5 回答 5

39

首先,您必须注意,您不能将 Jalali 日期放入 DateTime 对象中,您将在“1392/02/31”之类的日期中出现异常。

因此,您必须以字符串或自定义 DateTime 类型处理 Jalali 日期。

我建议您始终将日期保留为公历日期,并在需要显示时将其转换。

这是从 DateTime 对象获取波斯日期的扩展方法。

    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

然后当你有一个字符串格式的 Jalali 日期时,这里是获取公历日期的扩展方法:

    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}

现在来处理额外的功能:

周名称:

    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();

月份名称:

    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();
于 2012-02-29T00:10:50.330 回答
13

试试这样:

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

如果您将格式更改为例如"yyyy MMM ddd",您将获得2011 جوييه الأحد.

于 2011-07-24T14:04:04.190 回答
2

你可以使用这个:

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));
}
于 2011-07-27T13:10:20.607 回答
1

我创建了这个静态类 结果将以这种格式显示سه شنبه 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)}");
    }

}

用法

string ptime = DateTime.Now.ToPersianDateString();
于 2020-03-18T11:17:49.810 回答
1

这个接缝也有效:

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

输出:

1399/06/26
于 2020-09-16T03:56:38.143 回答