Click here to Skip to main content
13,837,307 members
Click here to Skip to main content
Add your own
alternative version

Stats

68.7K views
2.3K downloads
15 bookmarked
Posted 5 Dec 2014
Licenced CPOL

Persian Calendar(PersianDateTime) in C#

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

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:

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

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 :

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

Share

About the Author

Mohammad Dayyan
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

 
Praisethanks for your great Effort ! Pin
pedram roosta23-Feb-17 3:57
memberpedram roosta23-Feb-17 3:57 
QuestionSource code of the library? Pin
Shahin Khorshidnia11-Jun-16 1:13
professionalShahin Khorshidnia11-Jun-16 1:13 
AnswerRe: Source code of the library? Pin
Mohammad Dayyan11-Jun-16 4:14
memberMohammad Dayyan11-Jun-16 4:14 
GeneralRe: Source code of the library? Pin
Shahin Khorshidnia11-Jun-16 4:46
professionalShahin Khorshidnia11-Jun-16 4:46 
QuestionGreat Pin
flash-mmb17-Apr-16 5:25
memberflash-mmb17-Apr-16 5:25 
AnswerRe: Great Pin
Mohammad Dayyan17-Apr-16 23:14
memberMohammad Dayyan17-Apr-16 23:14 
BugDate Properties Pin
mohammad o6-Sep-15 21:56
membermohammad o6-Sep-15 21:56 
GeneralRe: Date Properties Pin
Mohammad Dayyan11-Oct-15 23:13
memberMohammad Dayyan11-Oct-15 23:13 
GeneralRe: Date Properties Pin
Mohammad Dayyan27-Oct-15 18:57
memberMohammad Dayyan27-Oct-15 18:57 
Questionهفته چندم ماه Pin
mohammad o26-Jul-15 6:50
membermohammad o26-Jul-15 6:50 
AnswerRe: هفته چندم ماه Pin
Mohammad Dayyan26-Jul-15 18:54
memberMohammad Dayyan26-Jul-15 18:54 
QuestionAdd comparable Pin
ali_amiri28-May-15 0:36
memberali_amiri28-May-15 0:36 
AnswerRe: Add comparable Pin
Mohammad Dayyan28-May-15 15:31
memberMohammad Dayyan28-May-15 15:31 
GeneralRe: Add comparable Pin
ali_amiri15-Jun-15 0:18
memberali_amiri15-Jun-15 0:18 
GeneralRe: Add comparable Pin
Mohammad Dayyan15-Jun-15 3:25
memberMohammad Dayyan15-Jun-15 3:25 
GeneralThanks Pin
mohammad o14-May-15 5:06
membermohammad o14-May-15 5:06 
GeneralRe: Thanks Pin
Mohammad Dayyan14-May-15 16:31
memberMohammad Dayyan14-May-15 16:31 
Questionhelp Pin
Member 1021206422-Jan-15 8:30
professionalMember 1021206422-Jan-15 8:30 
AnswerRe: help Pin
Mohammad Dayyan22-Jan-15 8:41
memberMohammad Dayyan22-Jan-15 8:41 
QuestionHELP Pin
Member 1021206420-Jan-15 9:07
professionalMember 1021206420-Jan-15 9:07 
AnswerRe: HELP Pin
Mohammad Dayyan20-Jan-15 18:26
memberMohammad Dayyan20-Jan-15 18:26 
BugMilliSecond Formatting Pin
Hossein Montazeri25-Dec-14 0:49
professionalHossein Montazeri25-Dec-14 0:49 
GeneralRe: MilliSecond Formatting Pin
Mohammad Dayyan26-Dec-14 16:13
memberMohammad Dayyan26-Dec-14 16:13 
AnswerRe: MilliSecond Formatting Pin
Mohammad Dayyan27-Dec-14 21:02
memberMohammad Dayyan27-Dec-14 21:02 
QuestionEng Pin
aminsaadatia23-Dec-14 23:11
memberaminsaadatia23-Dec-14 23: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190114.1 | Last Updated 12 Jan 2015
Article Copyright 2014 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid