Click here to Skip to main content
12,695,049 members (29,143 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


23 bookmarked

JQueryUI Datepicker in ASP.NET MVC

, 31 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Using Datepicker JQuery plugin in ASP.NET MVC

Datepicker is a nice and cool plugin for displaying the calendar with ease. It is very easy to use JQuery plugin, it comes as part of JQueryUI library, so if you want to use this – first download JQueryUI from and also download JQuery( if you haven’t done so yet.

For example, if you have a form like the one below:

<% using(Html.BeginForm()){%>
    <legend>Event Information</legend>
        <label for="EventName">Event Name:</label>
        <%= Html.TextBox("EventName")%>
            <label for="StartDate">Start Date:</label>
            <%= Html.TextBox("StartDate")%>
            <label for="EndDate">End Date:</label>
            <%= Html.TextBox("EndDate")%>
                <input type="submit" value="Save" />
<% }%>

and you want to attach datepicker to “StartDate” and “EndDate” input fields, what you need to do is call the datepicker function on the these input field selectors like below:

<script language="javascript">
    $(document).ready(function() {

This works fine as we expected. 

Difference in Date format patterns

Consider a scenario where your MVC application supports localization, then the selected date displayed in the input fields also should display in the same date format of the current culture(This format could be custom one or default one). This leads to another issue – Datepicker plugin given by the JQueryUI supports different date formats, but it is different from the one that is available in .NET. For e.g. in order to display a long day name (“Thursday”), .NET uses “dddd” it's equivalent in Datepicker is “DD”.

In order to solve this disparity between the .NET world and Datepicker world, I have created an HTML helper function, which could generate the Datepicker format from a .NET date format.

/// <span class="code-SummaryComment"><summary>

So how could we make use of this helper method? Just replace the datepicker initialization code we have written earlier with this:

<script language="javascript">
    $(document).ready(function() {
    $('#StartDate').datepicker({ dateFormat: '<%= Html.ConvertDateFormat() %>' });
    $('#EndDate').datepicker({ dateFormat: '<%= Html.ConvertDateFormat() %>' });

Hope this helps.


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


About the Author

Technical Lead
India India
I am someone who is passionate about programming. I started my career with classic asp and VB 6, later dived into the world of .NET. My ambition is to become a technical architect who could design complex systems in a simplistic form which obeys the "Laws of nature"

My personal blog

You may also be interested in...

Comments and Discussions

QuestionMessage Automatically Removed Pin
27-Oct-14 8:44
memberMd. Ilyas Hasan Mamun27-Oct-14 8:44 
SuggestionMessage Automatically Removed Pin
29-Apr-14 2:53
memberMember 1072790629-Apr-14 2:53 
Questionshows error when we convert yy to yyyy Pin
Preetika parolkar30-Mar-14 20:45
memberPreetika parolkar30-Mar-14 20:45 
GeneralMy vote of 1 Pin
shabarinadh.vempati3-Dec-13 21:09
professionalshabarinadh.vempati3-Dec-13 21:09 
GeneralMy vote of 4 Pin
Pie and Ale16-Jul-13 5:19
memberPie and Ale16-Jul-13 5:19 
Generalnice article Pin
Donsw1-Sep-12 9:10
memberDonsw1-Sep-12 9:10 
SuggestionA little suggestion Pin
Daniele Fusi1-Nov-11 10:19
memberDaniele Fusi1-Nov-11 10:19 
GeneralMy vote of 5 Pin
kiran dangar31-Oct-11 3:59
memberkiran dangar31-Oct-11 3:59 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 31 Oct 2011
Article Copyright 2010 by Rajeesh.C.V
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid