0

Currently I have an ASP.NET Core website and I need to have date field on the form and used this:

<input asp-for="StartDate" class="w-50" type="date" max="2020-10-23" min="1950-01-01" placeholder="dd/mm/yyyy" />

And used on the model the following:

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]        
    public DateTime? StartDate{ get; set; }

For some reason on en-US clients everything is overriden to MM/dd/yyyy. enter image description here

So the first question would be: can I force the date field to always use the correct format dd/MM/yyyy After searching SO I believe it can't be done, we cannot adjust the client behavior as it would go against the HTML rules.

Then another question comes in my mind. If it can't be done: How can I send the date correct to the backend? I don't care to be honest in what format the user enters the date. But when sending the form in an Ajax call, I need to make sure the date is in dd/MM/yyyy format. Now I use:

 jQuery.ajax({
        url: `/xxxxxxxx}`,
        type: "POST",
        data: $("#form").serialize(),
        success: function (partialView) {
            refreshForm(partialView);
        }
    });

The .serialize() just sends eg. 31/02/2020 or 02/31/2020 depending on the languange of the browser. I can't changed the API on the backend as it is not mine.

What would you do to fix this? Try to force the input to dd/MM/yyyy, try to parse the date in Javascript before sending(and how then) or even another solution?

I'm happy to hear you solution.

Regards, Miscode

Miscode
  • 57
  • 8
  • The only valid format for an input with type set to "date" is ISO-8601, which is YYYY-MM-DD. Anything else requires a text input. Is there a particular reason for requiring the format which you're trying to use? – Tieson T. Sep 23 '20 at 19:47
  • Hi @TiesonT. I'm not sure what you mean with your question? The reason for requiring the format I want to use is that nobody uses yyyy-MM-dd. We use dd-MM-yyyy and in the USA they use MM-dd-yyyy. Just to be sure. My question is about the date the user has to input, not about the min/max. Those need to be in yyyy-mm-dd format indeed but that is ok. – Miscode Sep 23 '20 at 20:15
  • "nobody uses yyyy-MM-dd" - well, I do, and I'm in the US. The HTML element itself, when setup as ``, uses ISO-8601, although what is displayed to the user varies by browser and regional settings. I'm kind of surprised, if you're saying serialize() is giving you a different value depending on the user's regional settings - that should just give you a Date() value. – Tieson T. Sep 24 '20 at 01:44
  • This is what I'm seeing: https://jsfiddle.net/xp2tb9un/ - it's always an ISO-8601 date. Granted, this isn't ASP.NET Core, but tag helpers just generate standard input elements, and the modelbinder isn't doing anything special – Tieson T. Sep 24 '20 at 01:58
  • I think you need to check [this answer](https://stackoverflow.com/a/60751537/11398810). – Rena Sep 24 '20 at 07:28
  • Hi @TiesonT. I've looked into your fiddle and indeed it seems your example serializes correct in the regions I tested. I add some console logging on my site to see what my js does... I think we are close to the root of the problem and it seems it is not the HTML ;) – Miscode Sep 24 '20 at 07:33

0 Answers0