5

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

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

4

2 回答 2

9

一种可能的方法是直接将 .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/

于 2011-01-21T05:30:58.000 回答
0

也许你可以使用这样的东西:

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

和这个:

$(function() {
    $("input.datebox").each(function() {
        $(this).datepicker({ dateFormat: $(this).attr("dateFormat") });
    });
});
于 2010-02-10T16:55:11.820 回答