【问题标题】:jQuery.ui.datepicker with Asp.Net MVC dateFormat mappingjQuery.ui.datepicker 与 Asp.Net MVC dateFormat 映射
【发布时间】:2011-01-15 08:49:45
【问题描述】:

是否有人将 c# dateFormat 映射到 datePicker dateFormat,因为我已经知道 C# dateFormat,所以我不想每次必须构建自定义日期格式时都检查 datepicker 文档。

例如,我希望能够在我的助手 dateFormat 中指定 'dd/MM/yy'(c#) 并将其转换为 'dd/mm/yy' DatePicker

【问题讨论】:

    标签: asp.net-mvc jquery-ui datepicker date-format


    【解决方案1】:

    一种可能的方法是直接将 .NET 格式说明符替换为对应的 jquery 格式说明符,您可以在以下代码中看到:

    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/

    【讨论】:

      【解决方案2】:

      也许你可以使用这样的东西:

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

      还有这个:

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

      【讨论】:

      • 这不是我想要的,我想要的是在c#中指定与datepicker中相同的格式,使用自定义的helper,所以转换可以在c#中daone,例如'dd datePicker 中的 /mm/yy' 在 c# 中是 'dd/MM/yy'。
      猜你喜欢
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 2011-04-10
      • 2010-11-08
      • 2014-11-13
      相关资源
      最近更新 更多