如何为 html table 的日期列提供日期格式 dd/mm/yyyy

How can give date format dd/mm/yyyy for the date column of an html table

我正在寻找如何为 table

的日期列提供 'dd/mm/yyyy' 格式
[DataType(DataType.Date, ErrorMessage = "Date only")]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
public string FromDate { get; set; }

<td>@Html.TextBoxFor(model => model.FromDate, new { @class = "form-control date-input emphrs" })</td>

你可以这样试试:

型号:

 public class DateFormaterModel
    {
        [DataType(DataType.Date, ErrorMessage = "Date only")]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public string FromDate { get; set; }

        [DataType(DataType.Date, ErrorMessage = "Date only")]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public string ToDate { get; set; }
    }

控制器:

public IActionResult DateFormatter()
        {
            return View();
        }

查看:

@model MVCApps.Models.DateFormaterModel

@{
    ViewData["Title"] = "DateFormatter";
}

<h2>Date Formatter</h2>

<table id="tblEntry" class="table table-striped">
    <thead>
        <tr>
            <th>Date From</th>
            <th>Date To</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>@Html.EditorFor(model => model.FromDate, new { @id = "fromDate", htmlAttributes = new { @class = "form-control datepicker w-100"} })</td>
            <td>@Html.EditorFor(model => model.ToDate, new { @id = "toDate", htmlAttributes = new { @class = "form-control datepicker w-100" } })</td>
        </tr>

    </tbody>
</table>

输出:

Note: You have missed this property ApplyFormatInEditMode = true which would help to achive that. Additionally as @LIama already said string wouldn't act here accordingly. So DateTime would be identical. But string can also would work perfectly the way I shown you.

希望能帮到您解决问题。