Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 442
dragonfly Avatar asked Dec 16 '11 07:12

dragonfly


1 Answers

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

like image 61
Michael Freidgeim Avatar answered Oct 15 '22 07:10

Michael Freidgeim