我在 .NET ASP MVC3 Intranet 应用程序中使用 jQuery 的 datepicker 插件。使用应用程序的用户在不同国家和不同地区设有办事处。这就是为什么我想将 Thread.CurrentThread.CurrentCulture.DateTimeFormat 与 jQuery datepicker 插件集成。我的第一个解决方案是创建辅助扩展方法:
public static string jQueryDatePickerFormat(this HtmlHelper helper)
{
return Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
}
并在 JavaScript 中设置 dateFormat 选项,如下所示:
$("#StartDate").datepicker({ dateFormat: '@Html.jQueryDatePickerFormat()' });
不久之后,我意识到 datepicker 的 dateFormat 选项支持的格式与 .NET 中的格式具有不同的实现。
例如:PL-PL 的 Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern 返回 yyyy-MM-dd (它将日期格式设置为 2010-01-01 ),而 datePicker 中的相同格式将格式化与 20102010 一月 01 相同的日期。我很快调整了我的帮助方法并应用了快速修复 Replace("yyyy", "yy").Replace("MM", "mm"):
public static string jQueryDatePickerFormat(this HtmlHelper helper)
{
return Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("yyyy", "yy").Replace("MM", "mm");
}
我工作,但我等待其他问题出现的那一刻。有没有简单的方法可以将 .NET 区域设置实现到 jQuery 的 datePicker 插件中?
谢谢,帕维尔
最佳答案
代码项目文章 ASP.NET MVC 中的 JQueryUI Datepicker http://www.codeproject.com/Articles/62031/JQueryUI-Datepicker-in-ASP-NET-MVC具有完全满足您想要的功能
/// Converts the .net supported date format current culture
/// format into JQuery Datepicker format.
/// </summary>
/// <param name="html">HtmlHelper object.</param>
/// <param name="format">Date format supported by .NET.</param>
/// <returns>Format string that supported in JQuery Datepicker.</returns>
public static string ConvertDateFormat(this HtmlHelper html, string format)
我还发布了一个执行相反操作的函数 - Translate jQuery UI Datepicker format to .Net Date format
关于jQuery datepicker 的 dateFormat - 如何与 .NET 当前文化 DateTimeFormat 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8531247/