5

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

2 Answers 2

8

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/

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you can use something like this:

<%= Html.TextBoxFor(x => x.SomeDate, new { @class = "datebox", dateformat = "dd/mm/yy" })%>

and this:

$(function() {
    $("input.datebox").each(function() {
        $(this).datepicker({ dateFormat: $(this).attr("dateFormat") });
    });
});

1 Comment

That's not what i want, what i want, is to specify the same format in c# than in the datepicker, using a custom helper, so the conversion can be daone in c#, for example, 'dd/mm/yy' in the datePicker is 'dd/MM/yy' in c#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.