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

How to Set PersianCalendar to CultureInfo

, 28 Dec 2008 LGPL3
Rate this:
Please Sign up or sign in to vote.
How to set PersianCalendar to CultureInfo


The Calendar property of DateTimeFormatInfo accepts only calendars that are valid for the culture associated with this instance of DateTimeFormatInfo. For example, if the current instance originated from the DateTimeFormatInfo property of a "fa-Ir" CultureInfo object, this property can accept only the calendars that are valid for the "fa-Ir" culture. The CultureInfo.Calendar property specifies the default calendar (GregorianCalendar) for the culture and the CultureInfo.OptionalCalendars property specifies all calendars supported by the culture (GregorianCalendar and HijriCalendar).

.NET Framework doesn't support Persian calendar for the culture. This culture doesn't accept the calendar; therefore display of DateTime in this culture is impossible.

A well known solution to solve this problem is declaring a new type like PersianDate. This type wraps DateTime. Developers should use it without functionalities of DateTime and other classes that depend on it. Therefore I present a solution to solve these problems.


For setting the calendar to culture, its OptionalCalendars should have a specified calendar. In Persian Culture, this calendar doesn't exist. This code raises an exception:

//this code raises an exception
info.DateTimeFormat.Calendar = new PersianCalendar();

Using the Code

Use PersianCultureHelper to set Persian calendars as:

//create an instance of culture
CultureInfo info = new CultureInfo(“fa-Ir”);
//set Persian option to specified culture

This class sets an instance of PersianCalendar to a specified culture by using reflection. It finds private fields of the culture and its DateTimeFormatInfo and sets correct values for them. With this solution, you don't need a new version of DateTime.

Using this solution, you achieve two advantages:

  • You can work with DateTime and display it without getting an exception:


  • You can enable .NET Framework features for the culture the same as ASP.NET calendar functionality like:


You can install “Persian Culture Package.msi” and write this code:

// create an instance of culture
CultureInfo info = new CultureInfo(“fa-Ir”);
//set Persian calendar to it without get exception
info.DateTimeFormat.Calendar = new PersianCalendar();

This setup enables .NET Framework’s custom culture only with copying fa-IR.nlp file to %SystemRoot%\Globalization. You can get more information about creating custom culture from MSDN.


  • 28th December, 2008: Initial post


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Reza Taroosheh
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

QuestionGet exception PinmemberMember 1033678225-Aug-14 6:49 
GeneralMy vote of 5 PinmemberArminss29-Nov-12 5:11 
QuestionThanks Pinmemberzolfaghari26-May-12 23:41 
GeneralMy vote of 4 Pinmembersamalan26-Oct-10 10:56 
GeneralCode access security Pinmemberarash.rohani25-Jun-10 6:09 
GeneralRe: Code access security [modified] PinmemberReza Taroosheh25-Jun-10 7:15 
GeneralRe: Code access security Pinmemberarash.rohani25-Jun-10 10:44 
GeneralRe: Code access security PinmemberReza Taroosheh25-Jun-10 11:33 
GeneralRe: Code access security Pinmemberarash.rohani25-Jun-10 21:42 
GeneralMy vote of 2 Pinmemberbluexp200429-Jan-10 4:27 

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 | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 28 Dec 2008
Article Copyright 2008 by Reza Taroosheh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid