2010-02-10 83 views
5

是否有某人做了一個映射的C#dateFormat datePicker dateFormat,因爲我已經知道C#dateFormat,我不想檢查datepicker文檔每次我必須建立一個自定義日期格式。jQuery.ui.datepicker與Asp.Net MVC dateFormat映射

爲exmple,我希望能夠在 'DD/MM/YY'(C#)的幫助我的日期格式來指定,它會轉換爲 'DD/MM/YY' 的DatePicker

回答

9

一種可能的方法是直接與他們同行的jQuery取代.NET格式說明,你可以在下面的代碼中看到:

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

原始來源:http://rajeeshcv.com/2010/02/28/JQueryUI-Datepicker-in-ASP-Net-MVC/

+0

Thx,看起來不錯,我現在必須嘗試... – 2011-01-21 13:29:00

0

也許你可以使用這樣的事情:

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

這:

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

這不是我想要的,我想要什麼,是指定在C#中相同的格式比在日期選擇器,使用自定義的幫手,所以轉換可以在C#daone ,例如,datePicker中的'dd/mm/yy'在c#中是'dd/MM/yy'。 – 2010-02-10 18:17:22