Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#
Tip/Trick

PersianCalendarPlus - An Easy to Use Tool for Working and Converting PersianDateTime to Gregorian and Back

Rate me:
Please Sign up or sign in to vote.
3.74/5 (5 votes)
9 Oct 2015CPOL1 min read 13.7K   52   2   5
PersianCalendarPlus is a simple wrapper around System.Globalization.PersianCalendar. In this tip, I'm going to show you how you can use it.

Introduction

PersianCalendarPlus is a simple wrapper around System.Globalization.PersianCalendar providing extra functionality such as different kinds of date string in Persian and the ability to convert between them.

How Can I Install It?

You can install it using Nuget package manager:

Install-Package <a href="https://www.nuget.org/packages/PersianCalendarPlus/">PersianCalendarPlus</a>

Where Can I Find the Source Code?

PersianCalendarPlus is open source, you can find the source code on Github: PersianCalendarPlus

Usage Example

C#
PerCalPlus.Now() // "1394/07/16 07:34:34 ب.ظ"
PerCalPlus.ToShortDateString() // "1394/07/16"
PerCalPlus.ToLongDateString() // "پنجشنبه - 16 مهر - 1394"

PerCalPlus.GetPersianYear() // "1394"
PerCalPlus.GetPersianYear(1988) // "1366"
PerCalPlus.GetPersianMonth() // "7"
PerCalPlus.GetPersianMonth(2, 21) // "12"
PerCalPlus.GetPersianDay() // "16"
PerCalPlus.GetPersianDay(1988, 2, 21) // "2"
PerCalPlus.GetDaysInPersianMonth(66, 12) // "30"
PerCalPlus.GetDaysInPersianYear(66) // "366"
PerCalPlus.GetDayOfWeekName() // "پنجشنبه"
PerCalPlus.GetDayOfWeekName(DateTime.Now.AddDays(2)) // "شنبه"
PerCalPlus.GetMonthName() // "مهر"
PerCalPlus.GetMonthName(DateTime.Now.AddDays(21)) // "آبان"

PerCalPlus.GregorianDateToPersian(1999, 8, 2) // "1378/05/11"
PerCalPlus.GregorianDateToPersian("5/27/2015 3:14:25 PM") // "1394/03/06"
PerCalPlus.GregorianDateToPersian(DateTime.Now) // "1394/07/16"
PerCalPlus.GregorianDateToPersianLong(DateTime.Now) // "پنجشنبه - 16 مهر - 1394"

PerCalPlus.PersianDateToGregorian(1366, 12, 2) // "2/21/1988 12:00:00 AM"
PerCalPlus.PersianDateToGregorian(1366, 12, 2, 14, 22, 15) // "2/21/1988 2:22:15 PM"
PerCalPlus.PersianDateToGregorian("1366/12/2", 
	PerCalPlus.DateStringType.ToShortDateString) // "2/21/1988 12:00:00" AM
PerCalPlus.PersianDateToGregorian("1394/07/13 08:44:28 ب.ظ", 
	PerCalPlus.DateStringType.Now) // "10/5/2015 12:00:00 AM"
PerCalPlus.PersianDateToGregorian("دوشنبه - 2 اسفند - 1366", 
	PerCalPlus.DateStringType.ToLongDateString) // "2/21/1988 12:00:00 AM"

PerCalPlus.ShortYearToLongYear(66) // "1366"
PerCalPlus.ShortYearToLongYear("66") // "1366"

Converting from Gregorian Date to Persian

There are two methods that you can use for this purpose, if you want short date, you can use one of these three overloads:

C#
PerCalPlus.GregorianDateToPersian(1999, 8, 2) // "1378/05/11"
PerCalPlus.GregorianDateToPersian("5/27/2015 3:14:25 PM") // "1394/03/06"
PerCalPlus.GregorianDateToPersian(DateTime.Now) // "1394/07/16"

If you need long string date, you can use:

C#
PerCalPlus.GregorianDateToPersianLong(DateTime.Now) // "پنجشنبه - 16 مهر - 1394"

Converting from Persian Date to Gregorian

There is one method for this with five overloads, two of them take int as argument:

C#
PerCalPlus.PersianDateToGregorian(1366, 12, 2) // "2/21/1988 12:00:00 AM"
PerCalPlus.PersianDateToGregorian(1366, 12, 2, 14, 22, 15) // "2/21/1988 2:22:15 PM"

The other three take a date string, note that you should specify what kind of date string you're supplying as argument through PerCalPlus.DateStringType enumeration, these three kind of strings are the same as the one created by these three methods:

C#
PerCalPlus.Now() // "1394/07/16 07:34:34 ب.ظ"
PerCalPlus.ToShortDateString() // "1394/07/16"
PerCalPlus.ToLongDateString() // "پنجشنبه - 16 مهر - 1394"

In the enum part of method argument, you should specify what kind of date string you want to supply as an argument, these three are PerCalPlus.DateStringType.ToShortDateString and PerCalPlus.DateStringType.Now and PerCalPlus.DateStringType.ToLongDateString which correspond to the aforementioned methods, note that the date string should conform to the format that produced previously by the PersianCalendarPlus or it isn't going to work:

C#
PerCalPlus.PersianDateToGregorian("1366/12/2", 
	PerCalPlus.DateStringType.ToShortDateString) // "2/21/1988 12:00:00 AM"

PerCalPlus.PersianDateToGregorian("1394/07/13 08:44:28 ب.ظ", 
	PerCalPlus.DateStringType.Now) // "10/5/2015 12:00:00 AM"

PerCalPlus.PersianDateToGregorian("دوشنبه - 2 اسفند - 1366", 
	PerCalPlus.DateStringType.ToLongDateString) // "2/21/1988 12:00:00 AM"

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
Programming is my passion, because I find it so intellectually rewarding. I currently work as a back-end web developer, using Microsoft technology stack, I also blog about my experiences and contribute to open source projects on my free time.

Comments and Discussions

 
Generalcould you attach source code here in this article? Pin
Southmountain9-Oct-15 6:32
Southmountain9-Oct-15 6:32 
GeneralRe: could you attach source code here in this article? Pin
Hamid Mosalla9-Oct-15 6:57
Hamid Mosalla9-Oct-15 6:57 
It is open source, you can find the source code on github:
github.com/HamidMosalla/PersianCalendarPlus

QuestionRe: could you attach source code here in this article? Pin
Paul Conrad9-Oct-15 7:23
professionalPaul Conrad9-Oct-15 7:23 
AnswerRe: could you attach source code here in this article? Pin
Hamid Mosalla9-Oct-15 7:36
Hamid Mosalla9-Oct-15 7:36 
GeneralRe: could you attach source code here in this article? Pin
Paul Conrad9-Oct-15 7:41
professionalPaul Conrad9-Oct-15 7:41 

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.