jquery.ui.datepicker带有ASP.NET MVC DateFormat映射
-
19-09-2019 - |
题
是否有人在某个地方对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-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") });
});
});
不隶属于 StackOverflow