I need to customize the razor date input format. I am using an aspnetcore 6.0 mvc application
I want the dutch input format , day-month-year, but I only succeed in getting the American input format month-day-year.
here is the form:
@model myViewModel
<form asp-action="Edit" method="post" role="form">
<div >
<label asp-for="myDateField" ></label>
<div >
<input type="date" asp-for="myDateField" />
</div>
</div>
<div >
<div >
<input type="submit" value="save" />
</div>
</div>
</form>
here is my viewModel:
public class myViewModel
{
[DisplayName("why won't the format change :'(")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? myDateField{ get; set; }
}
I have been playing around with the format settings and attributes. Like adding the DataType attribute, changing the format, but I'm not getting any success. Using these settings currently shown even hide the current viewmodel mydate value.
I did not expect this to be so difficult. I was hoping to find a global solution which i could add to the Program.cs file but I could not get anything to work.
Does anyone know whats going on here?
EDIT: I already set the culture to dutch, which doesn't seem to change anything. I used the following:
var defaultCulture = new CultureInfo("nl-NL");
defaultCulture.NumberFormat.CurrencySymbol = "€";
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(defaultCulture),
SupportedCultures = new List<CultureInfo> { defaultCulture },
SupportedUICultures = new List<CultureInfo> { defaultCulture }
};
app.UseRequestLocalization(localizationOptions);
Edit: answer update
As Ruikai Feng suggested, my system language settings where ultimately responsible for the date format. I did not need the DisplayFormat attribute.
CodePudding user response:
All C# codes in your razor view would be complied to html/js codes when you receive the response
And I found the explain in this case: in html 5 input date expects the date in the RFC3339 format
The date output will then be displayed to the user based on the users browser and their calendar settings.