Search code examples
c#htmlasp.net-core-mvcasp.net-core-6.0

What is the DateTime format of input type "datetime-local"


I have an ASP.NET Core 6.0 MVC project. I am getting date data from sql and show it in html page. Problem is, my date input is never filled up. HTML part is:

<input type="datetime-local" value="@Model.Date" class="inputwidth">

My date format from model is day.month.year 00:00:00

I used a couple of formats but none worked. What format should I use and how can I convert it?


Solution

  • It shouldn't matter, but it's ISO8601. If Model.Date is a DateTime binding just works.

    System.DateTime has no format, it's a binary value. In fact, internally the value is stored as a long tick count. Date types in all databases (except SQLite) don't have formats either, they're binary values as well.

    The input's Value will always be in ISO8601 form, no matter what text is displayed. From Mozilla's docs for input type='datetime-local'

    One thing to note is that the displayed date and time formats differ from the actual value; the displayed date and time are formatted according to the user's locale as reported by their operating system, whereas the date/time value is always formatted YYYY-MM-DDThh:mm. When the above value submitted to the server, for example, it will look like partydate=2017-06-01T08:30.

    Assuming the Date property is a DateTime, ASP.NET Core will bind its value to the input element's Value attribute and back automatically.

    The name datetime-local is used to remind developers that the input element represents the end user's time, not the server's or UTC. There's no datetime or datetime-UTC type.