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 http://jqueryui.com/download and also download JQuery(http://docs.jquery.com/Downloading_jQuery) if you haven’t done so yet.
For example, if you have a form like the one below:
<% using(Html.BeginForm()){%>
<fieldset>
<legend>Event Information</legend>
<p>
<label for="EventName">Event Name:</label>
<%= Html.TextBox("EventName")%>
</p>
<p>
<label for="StartDate">Start Date:</label>
<%= Html.TextBox("StartDate")%>
</p>
<p>
<label for="EndDate">End Date:</label>
<%= Html.TextBox("EndDate")%>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% }%>
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() {
$('#StartDate').datepicker();
$('#EndDate').datepicker();
});
</script>
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.
public static class JQueryUIDatePickerHelper
{
public static string ConvertDateFormat(this HtmlHelper html)
{
return ConvertDateFormat(html,
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern);
}
public static string ConvertDateFormat(this HtmlHelper html, string format)
{
string currentFormat = format;
currentFormat = currentFormat.Replace("dddd", "DD");
currentFormat = currentFormat.Replace("ddd", "D");
if (currentFormat.Contains("MMMM"))
{
currentFormat = currentFormat.Replace("MMMM", "MM");
}
else if (currentFormat.Contains("MMM"))
{
currentFormat = currentFormat.Replace("MMM", "M");
}
else if (currentFormat.Contains("MM"))
{
currentFormat = currentFormat.Replace("MM", "mm");
}
else
{
currentFormat = currentFormat.Replace("M", "m");
}
currentFormat = currentFormat.Contains("yyyy") ?
currentFormat.Replace("yyyy", "yy") : currentFormat.Replace("yy", "y");
return currentFormat;
}
}
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() %>' });
});
</script>
Hope this helps.