1

i want to get DayOfWeek in persianCalnder but it is always same as Georgian Calender,

       DateTime today = DateTime.Today;
       PersianCalendar pc = new PersianCalendar();

     var  geDayOfWeek= today.DayOfWeek;             
     var pcDayOfWeek=pc.GetDayOfWeek(today);

why the value of geDayOfWeek and pcDayOfWeek are always the same ? persian week starts in Saturday not Monday , so for example Sunday should be 2 not 0 .

any solution to get day number in Persian calendar ?

i need this to calculate start date and end date of week in Persian Calendar not Georgian Calendar.

Arash
  • 3,458
  • 7
  • 32
  • 50
  • See if this helps - http://stackoverflow.com/questions/30683664/how-convert-gregorian-date-to-persian-date – Sanket Jul 03 '16 at 10:38
  • no, i can get the date in persian calnder , but i cant get the day number in persian calender , for example if current day is sunday , the pc.GetDayOfWeek(today) should return 2 not 0 . – Arash Jul 03 '16 at 10:42

2 Answers2

5

You can write a extention method for DateTime to get Persion Day Of Week

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
    }
}

You can use it like this :

PersianDayOfWeek DoW = DateTime.Now.PersionDayOfWeek();
Kahbazi
  • 14,331
  • 3
  • 45
  • 76
  • thanks arvin , so what is use of GetDayOfWeek in persian calender ? – Arash Jul 03 '16 at 10:44
  • @Arash with this method you will get if your date is Sunday, you will get `PersianDayOfWeek.Yekshanbe` which is 2 , as you want. – Kahbazi Jul 03 '16 at 10:46
  • thanks . but microsoft should redesign persianCalenday , what is point of GetDayOfWeek in this calender. – Arash Jul 03 '16 at 10:57
3

Reason for behavior you see is that return value of Calendar.GetDayOfWeek is enum ie. constant - Sunday is always 0 independently from the Calendar you use. In other words returned value does not reflect which day is considered "start of the week" in specific calendar...

Michal Levý
  • 33,064
  • 4
  • 68
  • 86