2

I have a problem sending a date in the format DD/MM/YY. This all works fine if I use the YY/MM/DD format, but my application is specific to the UK so I wish to only support this format.

My controller method begins like this:

public IActionResult Index(DateTime startDate, DateTime endDate)

and the problem is that both date values are just coming in 0001-01-01 (DateTime.MinValue)

The controls are created in the .cshtml using the below.

@Html.TextBox("startDate", "", new { @class = "form-control datepicker", @placeholder = "dd/mm/yy" })

and this is wired up to a jQuery UI DatePicker. However I do not believe this is related to the issue as I said before, it works if I change to YY/MM/DD.

From findings on other questions on StackOverflow I have tried adding the following, but the issue still persists.

Startup.cs:

ConfigureServices Method:

services.Configure<RequestLocalizationOptions>(options =>
{
    options.DefaultRequestCulture = new RequestCulture("en-GB");
    options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-GB") };
    options.RequestCultureProviders.Clear();
});

Configure Method:

app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture("en-GB"),
    SupportedCultures = supportedCultures,
    SupportedUICultures = supportedCultures
});

I can confirm both of these are done before calling .AddMvc() / .UseMvc() as this was mentioned on another question.

I am a bit of a loss now, any ideas would be greatly appreciated.

5
  • This question is presented well; it's clear, gets down to the core issue, and effectively demonstrates your work. Thank you. Commented Mar 17, 2020 at 19:29
  • Here is exactly what you need: stackoverflow.com/a/46051222/2568863 Commented Mar 17, 2020 at 19:51
  • This one extended above answer: vickram.me/… Commented Mar 17, 2020 at 19:56
  • @Dongdong Thank you, this was not a question I came across, I will implement this tomorrow. Commented Mar 17, 2020 at 20:01
  • @Dongdong This worked perfectly, thank you. If you want to post this as an answer instead of a comment, I can accept it. Commented Mar 18, 2020 at 9:30

0

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.