Change default format for DateTime parsing in ASP.NET Core

前端 未结 10 1025
情话喂你
情话喂你 2021-02-02 08:57

I get a Date in an ASP.NET Core Controller like this:

public class MyController:Controller{
    public IActionResult Test(DateTime date) {

    }
}
10条回答
  •  一生所求
    2021-02-02 09:58

    I had same problem ad almost got mad. I tried everthing with no sucsses. First I found a workaround to solve part of my problem:

    Workaround:

    string data1 
    string horainicio 
    string horafim
    
    var ageData = new AgendaData();
    var user = await _userManager.GetUserAsync(User);
    string usuario = user.Id;
    int empresa = user.IdEmpresa;
    int Idprospect = Convert.ToInt32(prospect);
    int minutos = 0;           
    var tipoAgenda = TipoAgenda.Contato;
    
    var provider = CultureInfo.InvariantCulture;
    provider = new CultureInfo("en-US");            
    string formato = "dd/MM/yyyy HH:mm";
    
    var dataInicio = DateTime.ParseExact(data1 + " " + horainicio, formato, provider);
    var dataFim = DateTime.ParseExact(data1 + " " + horafim, formato, provider);           
    var dataAlerta = dataInicio.AddMinutes(-minutos);
    

    But, this way i aways have to set invariantculture to all my datetime. I found the solution setting my culture at the configure on startup.cs.

    Set Culture on startup.cs

     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CRMContext context)
            {
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();
    
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                    app.UseDatabaseErrorPage();
                    app.UseBrowserLink();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
    
                //Fixar Cultura para en-US
                RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions
                {
                    SupportedCultures = new List { new CultureInfo("en-US") },
                    SupportedUICultures = new List { new CultureInfo("en-US") },
                    DefaultRequestCulture = new RequestCulture("en-US")
                };
    
                app.UseRequestLocalization(localizationOptions);      
                app.UseStaticFiles();
                app.UseIdentity();
    
                // Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
    
                context.Database.EnsureCreated();
            }
    

    Hope this help you.

提交回复
热议问题