Click here to Skip to main content
Click here to Skip to main content

Persian DateTime

By , 25 Apr 2011
 

Introduction

After a long time, I updated this article again. (Happy!!!)

Since the time I can remember, lack of a class or struct to support Persian calendar in programming languages was one of my big problems. Up to the language I was using, I tried to solve this problem with different solutions. But none of them is an absolute solution.

This problem was almost solved in .NET 2.0 by a class named PersianCalendar, but it was not the complete way. So I decided to create a class like System.DateTime.

Background

PersianDateTime is a very simple struct which gives us simple support for Persian DateTime. I'm trying to develop and update it. Therefore it would be great if you leave your suggestions to help me complete it.

Using the Code

A few examples can be found in the source code.

PersianDateTime can be used in two ways: Staticly and instanciately. I mean you can use it with/without an instance:

Console.WriteLine(PersianDateTime.Now);

or:

PersianDateTime persianDateTime = new PersianDateTime(DateTime.Now);

License

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

About the Author

Mohammad Mir mostafa
Program Manager
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionshamsi Date to miladimemberMarziyeh Eslampanah20 Oct '12 - 0:16 
can any one help me about a Conversion of Shamsi date to Miladi?
i need a dll.I kmow that VS2010 has that but isnot work exact Frown | :( :(
QuestionSuggestion: Please use better names!memberFlomenol7 Apr '12 - 3:20 
Thanks a lot. Great tool. If I may give you a suggestion, that would be using better names for namespaces, classes,...
For example, I don't think Library35 is a descriptive or even a good name for the main namespace. Something like PersianDTLibrary or PDT would be better (in my opinion).
 
Thanks again for your efforts. Really appreciate it. Thumbs Up | :thumbsup:
AnswerRe: Suggestion: Please use better names!memberMohammad Mir mostafa8 Apr '12 - 23:23 
Thanks for your suggestion.
I have a almost big library from my own source codes.
This library is separeted to tow major parts:
Library35 which contains my sources for .NET 3.5
Library40 which contains my sources for .NET 4.0
That's becouse I've used this namespace.
The other namespaces are subsets of these namespaces. Somethings like "Globalization", "Helpers", "Win", "Win.Controls", and so on.
Thanks again.
QuestionCode UsagememberMember 827970830 Sep '11 - 9:53 
Hi, a friend asked me to help do a simple home page which displays both a Gregorian and a Persian Date in Farsi.
 
I am no HTML or Java programmer, I only do a bit of Visual Basic so I used a web builder like WebPlus to quickly build simple web pages without writing a single line of HTML code. Besides, I cannot read the Persian or Farsi language. English is my first language.
 
Please tell me how I can put your code into a HTML web page in order to display the Persian date. Don't need to display any calendar but just the current date, is it possible with your code?
 
Thank you in advance for your help.
 
cheers, Steve.
Questionthankful for youmemberamir 21 Aug '11 - 22:10 
thank you Rose | [Rose]
Generalhimemberraha_45524 Feb '11 - 2:53 
thanks a lot for your source code,good luck Smile | :)
GeneralMy vote of 5memberSayyeid Mostafa Hashemi21 Dec '10 - 3:50 
Thanks mir mostafa
I am hashemi and you in pasargad company
GeneralMonth namesmemberKamranBehzad14 Jun '10 - 19:01 
Hi,
Thank you for sharing. You saves me heaps of time.
 
If I may make a suggestion. The MonthNames property currently creates a ReadOnlyCollection every time it is called. I would recommend the following pattern instead, where the list is created the first time only:
 
internal static ReadOnlyCollection<string> monthNames;
 
public static ReadOnlyCollection<string> MonthNames
    {
      get
      {
        if (monthNames == null)
        {
          List<string> names = new List<string>
                                 {
                                     PersianMonth.Farvardin.StringValue(),
                                     PersianMonth.Ordibehesht.StringValue(),
                                     PersianMonth.Khordad.StringValue(),
                                     PersianMonth.Tir.StringValue(),
                                     PersianMonth.Mordad.StringValue(),
                                     PersianMonth.Shahrivar.StringValue(),
                                     PersianMonth.Mehr.StringValue(),
                                     PersianMonth.Aban.StringValue(),
                                     PersianMonth.Azar.StringValue(),
                                     PersianMonth.Dey.StringValue(),
                                     PersianMonth.Bahman.StringValue(),
                                     PersianMonth.Esfand.StringValue()
                                 };
          monthNames = new ReadOnlyCollection<string>(names);
        }
        return monthNames;
      }
    }
 
Regards
Kamran Behzad

GeneralRe: Month namesmemberMohammad Mir mostafa14 Jun '10 - 23:37 
Thank you so much for your suggestion, i'm goning to upload a new version of my code. As you see, this code is writen about 3 years ago.

GeneralAddDaysmembershervin12 Mar '10 - 18:20 
Hi
 
First, I would like to thank you for your new approach. I will be grateful, If you could explain about adding days in PersianDateTime, I mean like AddDays in real DateTime.
 
Thanks, MRS
GeneralButmemberMember 671450114 Nov '09 - 23:56 
Thanks, but how can i convert persian date to gregorian?
GeneralRe: ButmemberMohammad Mir mostafa16 Nov '09 - 2:34 
I updated the source code, please refer to the new one.
With thanks Rose | [Rose]
Generaltank youmembers_ahmadneya30 Dec '08 - 1:03 
از لطف جنابعالی متشکرم
GeneralThank YoumemberMember 193372919 Sep '08 - 20:40 
Thank You Smile | :)
GeneralThanksmemberB.Farivar8 Jul '08 - 23:54 
Dear mohammad
Thanks for your sample. If u let me i'm going to use it in my project with your signature Big Grin | :-D .
 
Best regards
 
Behrang Farivar
GeneralC sharp Code GeneratormemberHemant.Kamalakar19 Sep '07 - 4:34 
Sir,
Do u have source code for Csharp Code Generator?
If possible plz send it to
hemant.kamalakar@gmail.com

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 25 Apr 2011
Article Copyright 2007 by Mohammad Mir mostafa
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid