Click here to Skip to main content
13,001,454 members (59,876 online)
Click here to Skip to main content
Add your own
alternative version

Stats

4.7K views
2 bookmarked
Posted 9 Oct 2015

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

, 9 Oct 2015
Rate this:
Please Sign up or sign in to vote.
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

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:

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:

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:

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:

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:

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)

Share

About the Author

Hamid Mosalla
Software Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I like the unknown, that's why I watch a lot of sci-fi, but that also led to my fascination with technology. I'm curious about new things, I like to tinker with them, use them and know them. I always try to take the more elegant and challenging route. I always welcome changes, without change, being a programmer is boring. Currently I work as a web developer and mostly use Microsoft products for my daily job, but I'm eager to learn and use any kind of technology to do the job.

You may also be interested in...

Pro

Comments and Discussions

 
Generalcould you attach source code here in this article? Pin
Southmountain9-Oct-15 6:32
memberSouthmountain9-Oct-15 6:32 
GeneralRe: could you attach source code here in this article? Pin
Hamid Mosalla9-Oct-15 6:57
memberHamid Mosalla9-Oct-15 6:57 
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
memberHamid 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 9 Oct 2015
Article Copyright 2015 by Hamid Mosalla
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid