Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Using a Persian calender in .NET 4

4.57/5 (23 votes)
19 Jun 2013CPOL1 min read 69.2K   9.2K  
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)