Question

Is there somewhere somebody who did a mapping of the c# dateFormat to the datePicker dateFormat, since I already know the C# dateFormat, I don't want to have to check the datepicker documentation everytime I have to build a custom date Format.

for exmple, i want to be able to specify in my helper dateFormat of 'dd/MM/yy'(c#) and it would convert it to 'dd/mm/yy' DatePicker

Was it helpful?

Solution

One possible approach would be to directly replace the .NET format specifiers with their jquery counterparts as you can see in the following code:

public static string ConvertDateFormat(string format)
{
    string currentFormat = format;

    // Convert the date
    currentFormat = currentFormat.Replace("dddd", "DD");
    currentFormat = currentFormat.Replace("ddd", "D");

    // Convert month
    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");
    }

    // Convert year
    currentFormat = currentFormat.Contains("yyyy") ? currentFormat.Replace("yyyy", "yy") : currentFormat.Replace("yy", "y");

    return currentFormat;
}

Original source: http://rajeeshcv.com/2010/02/28/JQueryUI-Datepicker-in-ASP-Net-MVC/

OTHER TIPS

Maybe you can use something like this:

<%= Html.TextBoxFor(x => x.SomeDate, new { @class = "datebox", dateformat = "dd/mm/yy" })%>

and this:

$(function() {
    $("input.datebox").each(function() {
        $(this).datepicker({ dateFormat: $(this).attr("dateFormat") });
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top