In an ASP.NET Core project I have to display a (readonly) date in a specific format (say "dd/mm/yyyy HH:MM")
<div class="form-group">
<label asp-for="Date" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Date" readonly class="form-control" />
</div>
</div>
How can I do it, knowing that the Date
field is declared in the model like this
public DateTime Date { get { return this.Timestamp.DateTime; } }
?
ASP stands for Active Server Pages. ASP is a development framework for building web pages. ASP supports many different development models: Classic ASP.
By default, every layout must call RenderBody . Wherever the call to RenderBody is placed, the contents of the view will be rendered.
A Tag Helper Component is a Tag Helper that allows you to conditionally modify or add HTML elements from server-side code. This feature is available in ASP.NET Core 2.0 or later. ASP.NET Core includes two built-in Tag Helper Components: head and body . They're located in the Microsoft.
Try this.
<input asp-for="Date" asp-format="{0:dd/MM/yyyy HH:mm}" readonly class="form-control" />
Alternatively, you could decorate the property with DisplayFormatAttribute
.
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy HH:MM}")]
public DateTime Date { get { return this.Timestamp.DateTime; } }
The markup you have would not need to change.
Using ASP.Core TagHelpers as per your question;
[DataType(DataType.Date)]
public DateTime Date { get { return this.Timestamp.DateTime; } }
will format the input fields nicely in accordance with your locale setting. I haven't found a tag helper solution for any other tags, looks like hardcoding as per Win's answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With