Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
758 views
in Technique[技术] by (71.8m points)

asp.net mvc - jQuery datepicker's dateFormat - how to integrate with .NET current culture DateTimeFormat

I'm using jQuery's datepicker plugin in .NET ASP MVC3 intranet application. User that use application have offices in different countries and different locale. This is why I wanted to integrate Thread.CurrentThread.CurrentCulture.DateTimeFormat with jQuery datepicker plugin. My first solution was to create helper extension method:

    public static string jQueryDatePickerFormat(this HtmlHelper helper)
    {
        return Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
    }

and set dateFormat option in javascript like this:

$("#StartDate").datepicker({ dateFormat: '@Html.jQueryDatePickerFormat()' });

Soon after I realized that datepicker's dateFormat option supports formats that have different implementation from format in .NET.

For instance: Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern for pl-PL returns yyyy-MM-dd (it will format date as 2010-01-01), whereas the same format in datePicker will format the same date as 20102010 January 01. I quickly adapted my helper method and applied quick fix 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");
    }

I works, but I wait for the moment when other issues will emerge. Is there any simpley way to implement .NET locale settings into jQuery's datePicker plugin?

Thanks,Pawel

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The codeproject article JQueryUI Datepicker in ASP.NET MVC http://www.codeproject.com/Articles/62031/JQueryUI-Datepicker-in-ASP-NET-MVC has function that does exactly what you wanted

    /// 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)

I've also posted a function that does the opposite-Translate jQuery UI Datepicker format to .Net Date format


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...