Click here to Skip to main content
15,860,844 members
Articles / Web Development / ASP.NET
Tip/Trick

ASP.NET DatePicker (Persian/Gregorian)

Rate me:
Please Sign up or sign in to vote.
4.73/5 (19 votes)
28 Jul 2014CPOL2 min read 163.2K   8.5K   11   57
Just another ASP.NET Persian(Jalali/Shamsi/Solar) / Gregorian Datepicker

Introduction

This is just another DatePicker for ASP.NET that supports both Persian (Jalali/Shamsi/Solar) and Gregorian Calendar.

Image 1 Image 2
Image 3

Its JavaScript source code is taken from here.  

This version is not completed yet and I'm going to add more features to it in the near future! 

Properties

Version 1.1.1  (2012-11-13)  
  • Now it supports empty value.
Version 1.1  (2012-10-15)  
  • ReadOnly:  Indicates whether user can change the text or not.  
  • ShowWeekNumber: If "true", then the calendar will display week numbers. 
  • ShowOthers:  If set to "true", then days belonging to months overlapping with the currently displayed month will also be displayed in the calendar (but in a "faded-out" color). 
  • FirstDayOfWeek: Specifies which day is to be displayed as the first day of week. The end user can easily change this too, by clicking on the day name in the calendar header. 
  • OnSelect: function that gets called when a date is selected. You don't have to supply this (the default is generally okay). 
  • OnUpdate: If you supply a function handler here, it will be called right after the target field is updated with a new date. You can use this to chain 2 calendars, for instance to setup a default date in the second just after a date was selected in the first.  
  • OnClose: This handler will be called when the calendar needs to close. You don't need to provide one, but if you do, it's your responsibility to hide/destroy the calendar. You're on your own. 
Version 1.0  (2012-10-07) 
  • Text: Text that is shown in the textbox
  • Date: Selected Gregorian date
  • DatePersian: Selected Persian date
  • CalendarType: Indicates which calendar type (Persian/Gregorian) will be shown

Sample code

For showing the Persian Datepicker:

XML
<rhp:DatePicker ID="DatePicker1" runat="server" 
DatePersian="1391/07/14" CalendarType="Persian"></rhp:DatePicker>  

For showing the Gregorian Datepicker:

XML
<rhp:DatePicker ID="DatePicker2" runat="server" 
Date="2012-10-06" CalendarType="Gregorian"></rhp:DatePicker>  

For getting selected date in postback event:

C#
Label1.Text = "Text: " + DatePicker1.Text;
Label2.Text = "Date: " + DatePicker1.Date.ToString();
Label3.Text = "DatePersian: " + DatePicker1.DatePersian; 

Example for OnUpdate:

JavaScript
function onUpdate(calendar){
            var msg =
                    "<br/>Persian: Year: " + calendar.date.getJalaliFullYear() +
                    ", Month: " + (calendar.date.getJalaliMonth() + 1) +
                    ", Day: " + calendar.date.getJalaliDate() +
                    "<br/>Gregorian: Year: " + calendar.date.getFullYear() +
                    ", Month: " + calendar.date.getMonth() +
                    ", Day: " + calendar.date.getDate();

            logEvent("onUpdate Event: <br> Selected Date: " + 
            calendar.date.print('%Y/%m/%d', 'jalali') + msg);
        }; 

Example for OnSelect:

JavaScript
function onSelect(calendar, date) {
            // Beware that this function is called even if the end-user only
            // changed the month/year. In order to determine if a date was
            // clicked you can use the dateClicked property of the calendar:
            if (calendar.dateClicked) {
                var msg =
                        "<br/>Persian: Year: " + calendar.date.getJalaliFullYear() +
                        ", Month: " + (calendar.date.getJalaliMonth() + 1) +
                        ", Day: " + calendar.date.getJalaliDate() +
                        "<br/>Gregorian: Year: " + calendar.date.getFullYear() +
                        ", Month: " + calendar.date.getMonth() +
                        ", Day: " + calendar.date.getDate();

                $("#<%= DatePicker1.ClientID %>").val(date);
                logEvent("onSelect Event: <br> Selected Date: " + date + msg);
                calendar.hide();
                //calendar.callCloseHandler(); // this calls "onClose"
            }
        }; 

Example for OnClose:

JavaScript
function onClose(calendar) {
            logEvent("onClose Event");
            calendar.hide();
        }; 

License

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


Written By
Software Developer (Senior) Respina Network & Beyond IP Pool Tehran PoPSite
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

 
Questionلطفا اگر ممکنه نحوه استفاده از این کد رو هم بزارید Pin
mohammadjoon211-Dec-12 4:30
mohammadjoon211-Dec-12 4:30 
AnswerRe: لطفا اگر ممکنه نحوه استفاده از این کد رو هم بزارید Pin
Ruhollah Heidarpour14-Dec-12 7:07
professionalRuhollah Heidarpour14-Dec-12 7:07 
Questionاستفاده در ام وی سی Pin
Mbahreini25-Nov-12 10:58
Mbahreini25-Nov-12 10:58 
AnswerRe: استفاده در ام وی سی Pin
Ruhollah Heidarpour25-Nov-12 17:42
professionalRuhollah Heidarpour25-Nov-12 17:42 
QuestionEnglish in comments Pin
Wendelius12-Nov-12 20:13
mentorWendelius12-Nov-12 20:13 
Questionسوال Pin
qweqweqwew12-Nov-12 2:57
qweqweqwew12-Nov-12 2:57 
AnswerRe: سوال Pin
Ruhollah Heidarpour12-Nov-12 18:51
professionalRuhollah Heidarpour12-Nov-12 18:51 
GeneralMy vote of 5 Pin
ZRNCompany6-Nov-12 2:07
ZRNCompany6-Nov-12 2:07 
تشکر
very very good!
Questionشناسایی با مرور گرها Pin
arh668-Oct-12 9:13
arh668-Oct-12 9:13 
AnswerRe: شناسایی با مرور گرها Pin
Ruhollah Heidarpour9-Oct-12 20:45
professionalRuhollah Heidarpour9-Oct-12 20:45 

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.