JQuery.ui.datepicker with Asp.Net MVC date and time display

Is there anyone who has mapped C # dateFormat to datePicker dateFormat, since I already know C # dateFormat, I don’t want to check the datepicker documentation every time I need to create a custom Format date.

for exmple, I want to be able to specify in my helper dateFormat from 'dd / MM / yy' (C #) and it will convert it to 'dd / mm / yy' DatePicker

+5
source share
2 answers

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;
}

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

+9

, - :

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

:

$(function() {
    $("input.datebox").each(function() {
        $(this).datepicker({ dateFormat: $(this).attr("dateFormat") });
    });
});
0

All Articles