I am struggling to get the right date format in ASP.NET Core 2.1. I have already various solutions for several hours and simply can't get it to work. Things I have tried so far:
Setting the culture in Startup.cs
ConfigureServices
class:// Add localization services.AddLocalization(options => options.ResourcesPath = "Resources"); services.Configure<RequestLocalizationOptions>( opts => { var supportedCultures = new List<CultureInfo> { new CultureInfo("nl-NL"), }; opts.DefaultRequestCulture = new RequestCulture("nl-NL"); // Formatting numbers, dates, etc. opts.SupportedCultures = supportedCultures; // UI strings that we have localized. opts.SupportedUICultures = supportedCultures; opts.DefaultRequestCulture = new RequestCulture(culture: "nl-NL", uiCulture: "nl-NL"); }); Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
Setting the culture in Startup.cs
Configure
class:var supportedCultures = new[] { new CultureInfo("nl-NL") }; app.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("nl-NL"), SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures }); Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
Data annotation in viewmodel and normal model, the property first was a
DateTime
property but that also didn't work:[DisplayName("Datum van machtiging")] [Required(ErrorMessage = "Het veld '{0}' is verplicht")] [DataType(DataType.Date), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] public string Datum_van_machtiging { get; set; }
The HTML where it is getting called (if that matters):
<div class="form-group"> <label asp-for="Datum_van_machtiging" class="control-label"></label> <input asp-for="Datum_van_machtiging" class="form-control" /> <span asp-validation-for="Datum_van_machtiging" class="text-danger"></span> </div>
Set the right globalization for IIS express as I publish it to IIS.
My program.cs (If that matters because I publish to local IIS.)
public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseUrls(@"http://0.0.0.0:5000") .UseIISIntegration() .UseEnvironment("Production") .UseStartup<Startup>() .Build(); }
EDIT: I am getting this warning too by the way: http://prntscr.com/nxu1d6