【问题标题】:Format date in a View in ASP.NET Core MVC在 ASP.NET Core MVC 的视图中格式化日期
【发布时间】:2018-11-23 10:30:34
【问题描述】:

如何将日期格式设为"dd.MM.yyyy",因为无论我做什么,它总是"MM/dd/yyyy"

查看部分:

<div class="col-md-6 col-xs-6 pull-left">
    <label asp-for="@Model.RequestRegistrationDateFrom"></label>
    <div class="input-group date">
        <input type="text" asp-for="@Model.RequestRegistrationDateFrom" class="form-control js-datepicker pull-left" />
        <div class="input-group-append">
            <span class="input-group-text calendar-fa"></span>
        </div>
    </div>
</div>

模型属性:

[BindProperty(SupportsGet = true)]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "dd.MM.yyyy")]
[Display(Name = "Date from", ResourceType = typeof(string))]
public DateTime? RequestRegistrationDateFrom { get; set; }

【问题讨论】:

  • [DataType(DataType.Date)] 生成 type="date" 它将在浏览器文化中显示带有日期的浏览器 HTML5 日期选择器(您需要将 ApplyFormatInEditMode = true 添加到 [DisplayFormat] 并使用 ISO 格式 - 参考 @ 987654321@了解更多详情。
  • 如果您不想要浏览器的 HTML5 日期选择器,则删除 [DataType] 属性并将 asp-format="{0:dd.MM.yyyy}" 添加到 &lt;input&gt;
  • @StephenMuecke 这两种方法都不起作用:(
  • 那你没做好或者还有其他问题

标签: c# asp.net-core-mvc


【解决方案1】:

这应该有效:

[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]

【讨论】:

    【解决方案2】:

    我听说过你正在经历的这种情况。不幸的是,我自己没有得到它,也没有机会深入研究 DisplayFormat 属性背后的实际问题。

    在这种情况下,将格式化字符串传递给 value 属性是可行的。

    <input type="text" asp-for="RequestRegistrationDateFrom" value="@Model.RequestRegistrationDateFrom.ToString("dd.MM.yyyy")" class="form-control js-datepicker pull-left" />
    

    现在就试试吧。希望它也适用于您的情况。

    如果您在找出背后的问题后可以在此处发布解决方案,那就太棒了。我对此很感兴趣。

    【讨论】:

      【解决方案3】:

      这很简单兄弟,只需执行以下操作:

      [数据类型(数据类型.日期)]

      公共日期时间日期{获取;放; } = 日期时间.今天;

      【讨论】:

      • 那怎么显示为"dd.MM.yyyy"呢?
      【解决方案4】:

      在您的模型中:

      [Column(TypeName = "Date")]
      [DataType(DataType.Date)]
      [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
      public DateTime start_date { get; set; }
      

      在你看来:

      <td>@item.start_date.ToString("dd/MM/yyyy")</td>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-21
        • 1970-01-01
        • 2016-07-03
        • 1970-01-01
        • 2013-02-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多