簡體   English   中英

在 ASP.NET Core MVC 中的視圖中格式化日期

[英]Format date in a View in ASP.NET Core MVC

我怎樣才能使日期格式為"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; }

這應該有效:

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

我聽說過你正在經歷的這種情況。 不幸的是,我自己沒有得到它,也沒有機會深入研究 DisplayFormat 屬性背后的實際問題。

在這種情況下,將格式化字符串傳遞給 value 屬性有效。

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

暫時試試吧。 希望它也適用於您的情況。

如果您在找出背后的問題時可以在此處發布解決方案,那就太棒了。 我對它很感興趣。

很簡單的兄弟,只需執行以下操作:

[數據類型(數據類型.日期)]

公共日期時間日期{獲取; 放; } = 日期時間.今天;

在您的模型中:

[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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM