Click here to Skip to main content
13,402,670 members (42,835 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


17 bookmarked
Posted 11 Mar 2012

Using a Persian calender in .NET 4

, 19 Jun 2013
Rate this:
Please Sign up or sign in to vote.
How to use a Persian datetime picker and calendar in your applications.


The Persian calendar is used in most countries where Persian is spoken. Although some regions use different month names. The Persian calendar is the official calendar of Iran and Afghanistan, and is one of the alternative calendars in regions such as Kazakhstan and Tajikistan. The Persian calendar is based on a solar year, and is approximately 365 days long. A year cycles through four seasons, and a new year begins when the sun appears to cross the equator from the southern hemisphere to the northern hemisphere as viewed from the center of the Earth. The new year marks the first day of the month of Farvardin, which is the first day of spring in the northern hemisphere. Each of the first six months in the Persian calendar has 31 days, each of the next five months has 30 days, and the last month has 29 days in a common year and 30 days in a leap year. A leap year is a year that, when divided by 33, has a remainder of 1, 5, 9, 13, 17, 22, 26, or 30. For example, the year 1370 is a leap year because dividing it by 33 yields a remainder of 17. There are approximately 8 leap years in every 33-year cycle.

In .NET 4, we can't use Persian Date Time Picker or Calendar. I don't know why Microsoft does not allow us to use it.

Using the code

We use the following function to add a Persian calendar to a program:

public PersianCulture(string cultureName, bool useUserOverride): base(cultureName, useUserOverride)
    //Temporary Value for cal.
    cal = base.OptionalCalendars[0];

    //populating new list of optional calendars.
    var optionalCalendars = new List<Calendar>();
    optionalCalendars.Insert(0, new PersianCalendar());

    Type formatType = typeof (DateTimeFormatInfo);
    Type calendarType = typeof (Calendar);

    PropertyInfo idProperty = calendarType.GetProperty("ID", 
                              BindingFlags.Instance | BindingFlags.NonPublic);
    FieldInfo optionalCalendarfield = formatType.GetField("optionalCalendars",
                                           BindingFlags.Instance | BindingFlags.NonPublic);

    //populating new list of optional calendar ids
    var newOptionalCalendarIDs = new Int32[optionalCalendars.Count];
    for (int i = 0; i < newOptionalCalendarIDs.Length; i++)
        newOptionalCalendarIDs[i] = (Int32) idProperty.GetValue(optionalCalendars[i], null);

    optionalCalendarfield.SetValue(DateTimeFormat, newOptionalCalendarIDs);

    optionals = optionalCalendars.ToArray();
    cal = optionals[0];
    DateTimeFormat.Calendar = optionals[0];
    DateTimeFormat.MonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", 
          "تیر", "مرداد", "شهریور", "مهر", 
          "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
    DateTimeFormat.MonthGenitiveNames = new[] { "فروردین", "اردیبهشت", 
          "خرداد", "تیر", "مرداد", "شهریور", 
          "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
    DateTimeFormat.AbbreviatedMonthNames = new[] { "فروردین", "اردیبهشت", 
          "خرداد", "تیر", "مرداد", "شهریور", "مهر", 
          "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
    DateTimeFormat.AbbreviatedMonthGenitiveNames = new[] { "فروردین", "اردیبهشت", 
          "خرداد", "تیر", "مرداد", "شهریور", 
          "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };

    DateTimeFormat.AbbreviatedDayNames = new string[] { "ی", "د", 
          "س", "چ", "پ", "ج", "ش" };
    DateTimeFormat.ShortestDayNames = new string[] { "ی", "د", 
          "س", "چ", "پ", "ج", "ش" };
    DateTimeFormat.DayNames = new string[] { "یکشنبه", "دوشنبه", 
          "ﺳﻪشنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه" };

    DateTimeFormat.AMDesignator = "ق.ظ";
    DateTimeFormat.PMDesignator = "ب.ظ";
    DateTimeFormat.ShortDatePattern = "yyyy/MM/dd";
    DateTimeFormat.LongDatePattern = "yyyy/MM/dd";
    DateTimeFormat.SetAllDateTimePatterns(new[] {"yyyy/MM/dd"}, 'd');
    DateTimeFormat.SetAllDateTimePatterns(new[] {"dddd, dd MMMM yyyy"}, 'D');
    DateTimeFormat.SetAllDateTimePatterns(new[] {"yyyy MMMM"}, 'y');
    DateTimeFormat.SetAllDateTimePatterns(new[] {"yyyy MMMM"}, 'Y');

And in next step we add below code to our global.asax:

protected void Application_BeginRequest(object sender, EventArgs e)
    var persianCulture = new PersianCulture();
    Thread.CurrentThread.CurrentCulture = persianCulture;


  • 11 March 2010: Initial post. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Program Manager Novin Afzar
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Organisation

19 members

You may also be interested in...


Comments and Discussions

Questionerror in Program Pin
Member 1197306820-Jun-16 1:09
memberMember 1197306820-Jun-16 1:09 
PraiseGreat Pin
Zarei Javad6-Jun-16 6:23
memberZarei Javad6-Jun-16 6:23 
Questionتشکر Pin
ahp200923-May-15 20:43
memberahp200923-May-15 20:43 
QuestionHow to get original date in gegorian Pin
Member 1152577915-Mar-15 5:18
memberMember 1152577915-Mar-15 5:18 
QuestionC# Pin
WithoutBrain19943-Aug-14 4:59
memberWithoutBrain19943-Aug-14 4:59 
Questiondosent work in masterpage? Pin
partow7-Aug-13 3:49
memberpartow7-Aug-13 3:49 
QuestionTanks Messeage Pin
amin.javid26-Jun-13 3:42
memberamin.javid26-Jun-13 3:42 
QuestionThanks Message Pin
mahmood seyedkarimi20-Jun-13 0:22
membermahmood seyedkarimi20-Jun-13 0:22 
QuestionChanging the names of the months Pin
Shah Baidar10-Mar-13 2:36
memberShah Baidar10-Mar-13 2:36 
AnswerRe: Changing the names of the months Pin
aref.bozorgmehr29-May-13 8:49
grouparef.bozorgmehr29-May-13 8:49 
QuestionSame here Pin
Mohammad Mir mostafa13-Mar-12 1:47
memberMohammad Mir mostafa13-Mar-12 1:47 
Bugcorrect text of article Pin
AmirHashemZadeh11-Mar-12 12:11
memberAmirHashemZadeh11-Mar-12 12:11 
GeneralRe: correct text of article Pin
aref.bozorgmehr11-Mar-12 23:42
grouparef.bozorgmehr11-Mar-12 23:42 
AnswerRe: correct text of article Pin
AmirHashemZadeh12-Mar-12 3:19
memberAmirHashemZadeh12-Mar-12 3:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180218.2 | Last Updated 19 Jun 2013
Article Copyright 2012 by aref.bozorgmehr
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid