带有Asp.Net MVC dateFormat映射的jQuery.ui.datepicker

moi*_*eme 5 asp.net-mvc jquery-ui datepicker date-format

是否有人将c#dateFormat映射到datePicker dateFormat,因为我已经知道C#dateFormat,我不想每次必须构建自定义日期格式时都要检查datepicker文档.

例如,我希望能够在我的帮助器中指定'dd/MM/yy'(c#)的dateFormat,并将它转换为'dd/mm/yy'DatePicker

use*_*765 9

一种可能的方法是直接用他们的jquery副本替换.NET格式说明符,如下面的代码所示:

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;
}
Run Code Online (Sandbox Code Playgroud)

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