Click here to Skip to main content
15,885,200 members
Articles / Web Development / ASP.NET
Tip/Trick

Using a Persian calender in .NET 4

Rate me:
Please Sign up or sign in to vote.
4.57/5 (23 votes)
19 Jun 2013CPOL1 min read 67.5K   9.2K   17   16
How to use a Persian datetime picker and calendar in your applications.

Image 1

Introduction

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:

C#
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.AddRange(base.OptionalCalendars);
    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:

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

History

  • 11 March 2010: Initial post. 

License

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


Written By
Program Manager Novin Afzar
Iran (Islamic Republic of) Iran (Islamic Republic of)
I was born in 1990 in Ilam
I have been programming since 2005
it has been nearly three years that I have worked at my company
I'm really learning to love
Project I'm working on NOW is Gadgetfa.com
This is a Organisation

19 members

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 137790032-May-18 19:07
Member 137790032-May-18 19:07 
Questionerror in Program Pin
Member 1197306820-Jun-16 0:09
Member 1197306820-Jun-16 0:09 
PraiseGreat Pin
Zarei Javad6-Jun-16 5:23
Zarei Javad6-Jun-16 5:23 
Questionتشکر Pin
ahp200923-May-15 19:43
ahp200923-May-15 19:43 
QuestionHow to get original date in gegorian Pin
Member 1152577915-Mar-15 4:18
Member 1152577915-Mar-15 4:18 
QuestionC# Pin
WithoutBrain19943-Aug-14 3:59
WithoutBrain19943-Aug-14 3:59 
AnswerRe: C# Pin
Aref Bozorgmehr13-Apr-18 19:11
Aref Bozorgmehr13-Apr-18 19:11 
Questiondosent work in masterpage? Pin
partow7-Aug-13 2:49
partow7-Aug-13 2:49 
QuestionTanks Messeage Pin
amin.javid26-Jun-13 2:42
amin.javid26-Jun-13 2:42 
QuestionThanks Message Pin
mahmood seyedkarimi19-Jun-13 23:22
mahmood seyedkarimi19-Jun-13 23:22 
QuestionChanging the names of the months Pin
Shah Baidar10-Mar-13 1:36
Shah Baidar10-Mar-13 1:36 
AnswerRe: Changing the names of the months Pin
aref.bozorgmehr29-May-13 7:49
professionalaref.bozorgmehr29-May-13 7:49 
QuestionSame here Pin
Mohammad Mir mostafa13-Mar-12 0:47
Mohammad Mir mostafa13-Mar-12 0:47 
Bugcorrect text of article Pin
AmirHashemZadeh11-Mar-12 11:11
professionalAmirHashemZadeh11-Mar-12 11:11 
GeneralRe: correct text of article Pin
aref.bozorgmehr11-Mar-12 22:42
professionalaref.bozorgmehr11-Mar-12 22:42 
AnswerRe: correct text of article Pin
AmirHashemZadeh12-Mar-12 2:19
professionalAmirHashemZadeh12-Mar-12 2: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.