Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / C# 4.0

Persian Calendar(PersianDateTime) in C#

Rate me:
Please Sign up or sign in to vote.
4.93/5 (35 votes)
11 Jan 2015CPOL1 min read 108.3K   3.6K   14   28
A C# library to use PersianCalendar as easy as DateTime

Introduction

You can use this library for creating PersianDateTime as easy as using DateTime.
Actually, This library is a wrapper for DateTime and PersianCalendar.
This library can create and parse most of popular persian datetime strings.

The last version is on www.github.com/Mds92/MD.PersianDateTime

Background

I've seen before Mohammad Mir mostafa, PersianDateTime, It's great and you can use it too.

Using the code

Installing this library by nuget:

Install-Package MD.PersianDateTime

You can use it as the following:

C#
static void Main(string[] args)
{
	PersianDateTime persianDateTime = new PersianDateTime(DateTime.Now);
	Console.WriteLine(persianDateTime);
	Console.ReadKey();
}

Also, This Library can parse some persian datetime like the followings:

C#
13930914
"1393/09/14 12:20:30"
"93/1/1 3:15 ب.ظ"
"1393/02/01 02:03:10:30"
"1393/09/14 12:20:30:300"
"1393/09/14 12:20 ب.ظ"
"1393/09/14"
"د 24 آذر 1393 4:2:5:5 ب.ظ"
"24 آذر 1393"
"جمعه 93/2/1 ساعت 3:2 ب.ظ"
"جمعه 14 آذر 1393 ساعت 11:50:30 ب.ظ"
"جمعه 14 آذر 1393 ساعت 16:50:30"

You can use this class directly instead of DateTime

C#
static void Main(string[] args)
{
	PersianDateTime persianDateTime = PersianDateTime.Parse("1393/09/15 12:20:30");
	PrintDateTime(persianDateTime);
	Console.ReadKey();
}

static void PrintDateTime(DateTime dateTime)
{
	Console.WriteLine(dateTime);
}

You can change all numbers to Persian Numbers by setting EnglishNumber property to false(default is false)

Some outputs :

C#
/// <summary>
/// 1393/09/14   13:49:40
/// </summary>
public override string ToString()

/// <summary>
/// 1393/09/14
/// </summary>
public string ToShortDateString()

/// <summary>
/// 13930914
/// It's greate to save in sql if you want to save persian date in database
/// </summary>
public int ToShortDateInt()

/// <summary>
/// ج 14 آذر 93
/// </summary>
public string ToShortDate1String()

/// <summary>
/// جمعه، 14 آذر 1393
/// </summary>
public string ToLongDateString()

/// <summary>
/// جمعه، 14 آذر 1393 ساعت 13:50:27
/// </summary>
public string ToLongDateTimeString()

/// <summary>
/// جمعه، 14 آذر 1393 13:50
/// </summary>
public string ToShortDateTimeString()

/// <summary>
/// 01:50 ب.ظ
/// </summary>
public string ToShortTimeString()

/// <summary>
/// 01:50:20 ب.ظ
/// </summary>
public string ToLongTimeString()

/// <summary>
/// Supported format
/// yyyy: سال چهار رقمی
/// yy: سال دو رقمی
/// MMMM: نام فارسی ماه
/// MM: عدد دو رقمی ماه
/// M: عدد یک رقمی ماه
/// dddd: نام فارسی روز هفته
/// dd: عدد دو رقمی روز ماه
/// d: عدد یک رقمی روز ماه
/// HH: ساعت دو رقمی با فرمت 00 تا 24
/// H: ساعت یک رقمی با فرمت 0 تا 24
/// hh: ساعت دو رقمی با فرمت 00 تا 12
/// h: ساعت یک رقمی با فرمت 0 تا 12
/// mm: عدد دو رقمی دقیقه
/// m: عدد یک رقمی دقیقه
/// ss: ثانیه دو رقمی
/// s: ثانیه یک رقمی
/// fff: میلی ثانیه 3 رقمی
/// ff: میلی ثانیه 2 رقمی
/// f: میلی ثانیه یک رقمی
/// tt: ب.ظ یا ق.ظ
/// t: حرف اول از ب.ظ یا ق.ظ
/// </summary>
public string ToString(string format)

/// <summary>
/// نمایش زمان سپری شده از زمان حال 
/// مثال: 2 دقیقه قبل 
/// </summary>
public string ElapsedTime()

/// <summary>
/// نمایش فقط تاریخ
/// مثال: 2014/04/13
/// </summary>
public DateTime Date

History

First post: 6th December 2014
7th December 2014 : Updated source code
8th December 2014 : Updated source code
9th December 2014 : Updated source code ( Adding format ) 
17th December 2014 : Improved parser
19th December 2014 : fixed some bugs
21th December 2014 : added some methods
22th December 2014 : Updated source code
23th December 2014 : Updated source code
27th December 2014 : Updated source code
3th January 2015: Supporting arabic chars in parsing
12th January 2015: Added some useful properties
13th April 2015: Added Date property
17th May 2015: Coverted to struct and fixed some bugs
11th July 2015: GetStartDayOfRamadan method added and fixed some bugs
27th July 2015: fix a bug in `GetWeekOfMonth`
5th Septembar 2015: fixed some bugs
 

 

License

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


Written By
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

 
Praisethanks for your great Effort ! Pin
pedram roosta23-Feb-17 2:57
pedram roosta23-Feb-17 2:57 
QuestionSource code of the library? Pin
Shahin Khorshidnia11-Jun-16 0:13
professionalShahin Khorshidnia11-Jun-16 0:13 
AnswerRe: Source code of the library? Pin
Mohammad Dayyan11-Jun-16 3:14
Mohammad Dayyan11-Jun-16 3:14 
GeneralRe: Source code of the library? Pin
Shahin Khorshidnia11-Jun-16 3:46
professionalShahin Khorshidnia11-Jun-16 3:46 
QuestionGreat Pin
flash-mmb17-Apr-16 4:25
flash-mmb17-Apr-16 4:25 
AnswerRe: Great Pin
Mohammad Dayyan17-Apr-16 22:14
Mohammad Dayyan17-Apr-16 22:14 
BugDate Properties Pin
momt996-Sep-15 20:56
momt996-Sep-15 20:56 
GeneralRe: Date Properties Pin
Mohammad Dayyan11-Oct-15 22:13
Mohammad Dayyan11-Oct-15 22:13 
GeneralRe: Date Properties Pin
Mohammad Dayyan27-Oct-15 17:57
Mohammad Dayyan27-Oct-15 17:57 
Questionهفته چندم ماه Pin
momt9926-Jul-15 5:50
momt9926-Jul-15 5:50 
AnswerRe: هفته چندم ماه Pin
Mohammad Dayyan26-Jul-15 17:54
Mohammad Dayyan26-Jul-15 17:54 
QuestionAdd comparable Pin
ali_amiri27-May-15 23:36
ali_amiri27-May-15 23:36 
AnswerRe: Add comparable Pin
Mohammad Dayyan28-May-15 14:31
Mohammad Dayyan28-May-15 14:31 
GeneralRe: Add comparable Pin
ali_amiri14-Jun-15 23:18
ali_amiri14-Jun-15 23:18 
GeneralRe: Add comparable Pin
Mohammad Dayyan15-Jun-15 2:25
Mohammad Dayyan15-Jun-15 2:25 
GeneralThanks Pin
momt9914-May-15 4:06
momt9914-May-15 4:06 
GeneralRe: Thanks Pin
Mohammad Dayyan14-May-15 15:31
Mohammad Dayyan14-May-15 15:31 
Questionhelp Pin
Member 1021206422-Jan-15 7:30
professionalMember 1021206422-Jan-15 7:30 
AnswerRe: help Pin
Mohammad Dayyan22-Jan-15 7:41
Mohammad Dayyan22-Jan-15 7:41 
QuestionHELP Pin
Member 1021206420-Jan-15 8:07
professionalMember 1021206420-Jan-15 8:07 
AnswerRe: HELP Pin
Mohammad Dayyan20-Jan-15 17:26
Mohammad Dayyan20-Jan-15 17:26 
BugMilliSecond Formatting Pin
Hossein Montazeri24-Dec-14 23:49
professionalHossein Montazeri24-Dec-14 23:49 
GeneralRe: MilliSecond Formatting Pin
Mohammad Dayyan26-Dec-14 15:13
Mohammad Dayyan26-Dec-14 15:13 
AnswerRe: MilliSecond Formatting Pin
Mohammad Dayyan27-Dec-14 20:02
Mohammad Dayyan27-Dec-14 20:02 
QuestionEng Pin
aminsaadatia23-Dec-14 22:11
aminsaadatia23-Dec-14 22:11 

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.