Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / Windows Forms
Article

How to Set PersianCalendar to CultureInfo

Rate me:
Please Sign up or sign in to vote.
4.33/5 (26 votes)
28 Dec 2008LGPL32 min read 87.4K   6K   18   18
How to set PersianCalendar to CultureInfo

Introduction

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.

Background

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:

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

Using the Code

Use PersianCultureHelper to set Persian calendars as:

C#
//create an instance of culture
CultureInfo info = new CultureInfo("fa-Ir");
//set Persian option to specified culture
PersianCultureHelper.SetPersianOptions(info);

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:

    untitled.JPG

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

    untitled1.JPG

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

C#
// 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.

History

  • 28th December, 2008: Initial post

License

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


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGet exception Pin
Member 1033678225-Aug-14 5:49
Member 1033678225-Aug-14 5:49 
GeneralMy vote of 5 Pin
Arminss29-Nov-12 4:11
Arminss29-Nov-12 4:11 
QuestionThanks Pin
zolfaghari26-May-12 22:41
zolfaghari26-May-12 22:41 
GeneralMy vote of 4 Pin
samalan26-Oct-10 9:56
samalan26-Oct-10 9:56 
GeneralCode access security Pin
arash.rohani25-Jun-10 5:09
arash.rohani25-Jun-10 5:09 
GeneralRe: Code access security [modified] Pin
Reza Taroosheh25-Jun-10 6:15
Reza Taroosheh25-Jun-10 6:15 
GeneralRe: Code access security Pin
arash.rohani25-Jun-10 9:44
arash.rohani25-Jun-10 9:44 
GeneralRe: Code access security Pin
Reza Taroosheh25-Jun-10 10:33
Reza Taroosheh25-Jun-10 10:33 
GeneralRe: Code access security Pin
arash.rohani25-Jun-10 20:42
arash.rohani25-Jun-10 20:42 
GeneralMy vote of 2 Pin
bluexp200429-Jan-10 3:27
bluexp200429-Jan-10 3:27 
Questionm_cultureTableRecord Pin
moradidn28-Dec-09 21:44
moradidn28-Dec-09 21:44 
AnswerSet Persion Culture for .NET Framework 4.0 [modified] Pin
moradidn3-Feb-10 1:47
moradidn3-Feb-10 1:47 
Generalerror shamsidate calendar,schedul, datetimepicker in telerikcomponent Pin
tabacodprj14-Jul-09 20:53
tabacodprj14-Jul-09 20:53 
salam hamvatan omidvaram halet khoob bashe man ye soal darm raje be componente telerik.
ma az in codi ke shoma dadid dar telerik component use kardam va calendare un shamsi shod hala ye moshkeli tooye datetimepicker , schedule dare ke erroresh ro baratoon mizaram haminja. lotfan rahnamee konid ke man chetori mitoonam in moshkel ro bar taraf konam.
albate hengame view dorost amal mikone vali moqe change shodan ba moshkele zir bar khord mikone.

ERROR:
Specified time is not supported in this calendar. It should be between 03/21/0622 00:00:00 (Gregorian date) and 12/31/9999 23:59:59 (Gregorian date), inclusive.
Parameter name: time

Wink | ;)
(dateshamsi = 1388/04/24)
GeneralRe: error shamsidate calendar,schedul, datetimepicker in telerikcomponent Pin
Reza Taroosheh17-Jul-09 2:58
Reza Taroosheh17-Jul-09 2:58 
Generalthanksgiving Pin
Sayehboon11-Mar-09 23:27
Sayehboon11-Mar-09 23:27 
Generalthere is problem in your submitted Code and NLP Pin
Saeed Tabrizi8-Jan-09 2:33
Saeed Tabrizi8-Jan-09 2:33 
AnswerRe: there is problem in your submitted Code and NLP Pin
Reza Taroosheh8-Jan-09 9:23
Reza Taroosheh8-Jan-09 9:23 
GeneralMy vote of 1 Pin
Rob Graham28-Dec-08 12:53
Rob Graham28-Dec-08 12:53 

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.