1

I have an ASP.NET Core MVC application with a view model which has a DateTime property for "Date of Birth":

[Required]
public DateTime DateOfBirth { get; set; }

I want to let the user to pick/enter date in dd/mm/yyyy format.

In the view originally I was using html Date type and it was working fine in Chrome but some issues in Edge and not working in IE 11 at all. Therefore I have decided to replace it with the jQuery DatePicker:

<input class="form-control datepicker" name="DateOfBirth" type="text" required>

In Js file:

$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });

It all works fine as long as I pass value as mm/dd/yyyy format (12/12/2017). As soon as I change it to 13/12/2017 (dd/mm/yyyy) I get server side error saying that method arguments are not valid.

4
  • 1
    You need to change the culture on the server to one that accepts dates in dd/MM/yyyy format (or you need to create a custom ModelBinder to convert the dates to your format Commented Jul 7, 2018 at 6:45
  • How to change culture on the server in asp net core? Commented Jul 7, 2018 at 7:07
  • In your web.config file, in the <system.web> section, add <globalization culture="en-AU" uiCulture="en-AU" /> (or what ever culture you want) Commented Jul 7, 2018 at 7:09
  • thanks! that did the trick. Although for asp core I needed to set the cultureInfo in the Startup file. you may post your comment as answer and I tick it as my answer :) Commented Jul 7, 2018 at 8:06

2 Answers 2

1
<input class="form-control datepicker" name="DateOfBirth" type="text" required data-date-format="dd-mm-yyyy">

You must use date-date-format:"dd-mm-yyyy"

Sign up to request clarification or add additional context in comments.

Comments

0

You need to set the culture of your server to one that accepts dates in dd/MM/yyyy format.

To set the default culture in your startup.cs file, in the ConfigureServices method (note the following sets the culture to en-AU (Australia))

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<RequestLocalizationOptions>(options =>
    {
        options.DefaultRequestCulture = new RequestCulture("en-AU");
    });
    services.AddMvc();
}

and in the Configure method

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseRequestLocalization();
    app.UseMvc();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.