Click here to Skip to main content
Licence LGPL3
First Posted 28 Dec 2008
Views 19,585
Downloads 1,102
Bookmarked 16 times

How to Set PersianCalendar to CultureInfo

By | 28 Dec 2008 | Article
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:

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

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

About the Author

Reza Taroosheh

Web Developer

Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThanks Pinmemberzolfaghari22:41 26 May '12  
GeneralMy vote of 4 Pinmembersamalan9:56 26 Oct '10  
GeneralCode access security Pinmemberarash.rohani5:09 25 Jun '10  
GeneralRe: Code access security [modified] PinmemberReza Taroosheh6:15 25 Jun '10  
GeneralRe: Code access security Pinmemberarash.rohani9:44 25 Jun '10  
GeneralRe: Code access security PinmemberReza Taroosheh10:33 25 Jun '10  
GeneralRe: Code access security Pinmemberarash.rohani20:42 25 Jun '10  
GeneralMy vote of 2 Pinmemberbluexp20043:27 29 Jan '10  
Questionm_cultureTableRecord Pinmembermoradidn21:44 28 Dec '09  
AnswerSet Persion Culture for .NET Framework 4.0 [modified] Pinmembermoradidn1:47 3 Feb '10  
Generalerror shamsidate calendar,schedul, datetimepicker in telerikcomponent Pinmembertabacodprj20:53 14 Jul '09  
GeneralRe: error shamsidate calendar,schedul, datetimepicker in telerikcomponent PinmemberReza Taroosheh2:58 17 Jul '09  
Generalthanksgiving PinmemberSayehboon23:27 11 Mar '09  
Generalthere is problem in your submitted Code and NLP PinmemberSaeed Tabrizi2:33 8 Jan '09  
AnswerRe: there is problem in your submitted Code and NLP PinmemberReza Taroosheh9:23 8 Jan '09  
GeneralMy vote of 1 PinmemberRob Graham12:53 28 Dec '08  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 28 Dec 2008
Article Copyright 2008 by Reza Taroosheh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid