在c#的特定日历(PersianCalender)中获取dayofweek

本文关键字:PersianCalender 获取 dayofweek 日历 | 更新日期: 2023-09-27 17:54:28

我想得到DayOfWeek在persiancalender,但它总是相同的格鲁吉亚日历,

       DateTime today = DateTime.Today;
       PersianCalendar pc = new PersianCalendar();
     var  geDayOfWeek= today.DayOfWeek;             
     var pcDayOfWeek=pc.GetDayOfWeek(today);

为什么geDayOfWeek和pcDayOfWeek的值总是一样的?波斯周从星期六开始,而不是星期一,所以例如星期日应该是2而不是0。

在波斯日历中获得日期的任何解决方案?

我需要这个来计算波斯日历的开始日期和结束日期,而不是格鲁吉亚日历。

在c#的特定日历(PersianCalender)中获取dayofweek

您可以为DateTime编写一个扩展方法来获取每周的日期

public static class DateTimeHelper
{
    public static PersianDayOfWeek PersionDayOfWeek(this DateTime date)
    {
        switch (date.DayOfWeek)
        {
            case DayOfWeek.Saturday:
                return PersianDayOfWeek.Shanbe;
            case DayOfWeek.Sunday:
                return PersianDayOfWeek.Yekshanbe;
            case DayOfWeek.Monday:
                return PersianDayOfWeek.Doshanbe;
            case DayOfWeek.Tuesday:
                return PersianDayOfWeek.Seshanbe;
            case DayOfWeek.Wednesday:
                return PersianDayOfWeek.Charshanbe;
            case DayOfWeek.Thursday:
                return PersianDayOfWeek.Panjshanbe;
            case DayOfWeek.Friday:
                return PersianDayOfWeek.Jome;
            default:
                throw new Exception();
        }
    }
    public enum PersianDayOfWeek
    {
        Shanbe=1,
        Yekshanbe=2,
        Doshanbe=3,
        Seshanbe=4,
        Charshanbe=5,
        Panjshanbe=6,
        Jome=7
    }
}

你可以这样使用:

PersianDayOfWeek DoW = DateTime.Now.PersionDayOfWeek();

您看到的行为的原因是Calendar.GetDayOfWeek的返回值是enum ie。constant -周日总是0,与您使用的日历无关。换句话说,返回值不反映在特定日历中哪一天被认为是"一周的开始"…