Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

Persian Calendar(PersianDateTime) in C#

4.93/5 (35 votes)
11 Jan 2015CPOL1 min read 111.9K   3.6K  
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)