
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:
public PersianCulture(string cultureName, bool useUserOverride): base(cultureName, useUserOverride)
{
cal = base.OptionalCalendars[0];
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);
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 = "ب.ظ";
}
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;
}
History
- 11 March 2010: Initial post.