Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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.

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)
{
    //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:

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)

Share

About the Author

aref.bozorgmehr
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
Group type: Organisation

19 members


Comments and Discussions

 
QuestionC# PinmemberWithoutBrain19943-Aug-14 3:59 
Questiondosent work in masterpage? Pinmemberpartow7-Aug-13 2:49 
QuestionTanks Messeage Pinmemberamin.javid26-Jun-13 2:42 
QuestionThanks Message Pinmembermahmood seyedkarimi19-Jun-13 23:22 
QuestionChanging the names of the months PinmemberShah Baidar10-Mar-13 1:36 
AnswerRe: Changing the names of the months Pingrouparef.bozorgmehr29-May-13 7:49 
QuestionSame here PinmemberMohammad Mir mostafa13-Mar-12 0:47 
Bugcorrect text of article PinmemberAmirHashemZadeh11-Mar-12 11:11 
GeneralRe: correct text of article Pingrouparef.bozorgmehr11-Mar-12 22:42 
AnswerRe: correct text of article PinmemberAmirHashemZadeh12-Mar-12 2:19 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 19 Jun 2013
Article Copyright 2012 by aref.bozorgmehr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid