Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ui.datepicker with Asp.Net MVC dateFormat mapping

Is there somewhere somebody who did a mapping of the c# dateFormat to the datePicker dateFormat, since I already know the C# dateFormat, I don't want to have to check the datepicker documentation everytime I have to build a custom date Format.

for exmple, i want to be able to specify in my helper dateFormat of 'dd/MM/yy'(c#) and it would convert it to 'dd/mm/yy' DatePicker

like image 750
moi_meme Avatar asked Feb 10 '10 16:02

moi_meme


1 Answers

One possible approach would be to directly replace the .NET format specifiers with their jquery counterparts as you can see in the following code:

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;
}

Original source: http://rajeeshcv.com/2010/02/28/JQueryUI-Datepicker-in-ASP-Net-MVC/

like image 64
user505765 Avatar answered Oct 18 '22 11:10

user505765