문제

c# dateformat를 datepicker dateformat에 매핑 한 사람이 있습니까? c# dateformat을 이미 알고 있기 때문에 사용자 정의 날짜 형식을 작성해야 할 때마다 DatePicker 문서를 확인하고 싶지 않습니다.

Exmple의 경우 'DD/MM/YY'(C#)의 헬퍼 DateFormat에 지정할 수 있고 'DD/MM/YY'DatePicker로 변환합니다.

도움이 되었습니까?

해결책

가능한 한 가지 방법은 다음 코드에서 볼 수 있듯이 .NET 형식 지정자를 jQuery 상대로 직접 교체하는 것입니다.

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/

다른 팁

어쩌면 다음과 같은 것을 사용할 수 있습니다.

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

이:

$(function() {
    $("input.datebox").each(function() {
        $(this).datepicker({ dateFormat: $(this).attr("dateFormat") });
    });
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top