title: How do I control the time format in an input field?
body: <p>My model has a datetime-field:</p>
  
  <pre><code>public DateTime ProjectStart { get; set; }
  </code></pre>
  
  <p>If I want the time to be displayed in 24-hours format, as plain text in a view, I can do this (with capital HH):</p>
  
  <pre><code>@Model.ProjectStart.ToString("dd/MM/yyyy HH:mm")
  </code></pre>
  
  <p>But how do I control the time format in an input field?</p>
  
  <pre><code>&lt;input asp-for="ProjectStart" class="form-control" /&gt;
  </code></pre>
  
  <p>The time is stored in the database 24 hour formatted. It is just the input field that is displaying it wrong. It should display date and time in this format:</p>
  
  <blockquote>
    <p>dd/MM/yyyy HH:mm (e.g. 28/02/2019 17:45)</p>
  </blockquote>
  
  <p>My app's culture (<code>CultureInfo.CurrentCulture</code>) is <code>nb-NO</code>, which is correct. The standard format for date and time in this culture is the one I'm trying to display in the input fields. (<a href="https://en.wikipedia.org/wiki/Date_and_time_notation_in_Norway" rel="nofollow noreferrer">Norwegian date and time formats</a>)</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have tried two solutions, with no luck:</p>
  
  <p>Decorating the property in the viewmodel:</p>
  
  <pre><code>[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy HH:mm}")]
  </code></pre>
  
  <p>Adding the tag helper attribute <code>asp-format</code> in the input tag:</p>
  
  <pre><code>&lt;!-- I tried several variations of this: --&gt;
  &lt;input asp-for="ProjectStart" asp-format="{0:dd/MM/yyyy HH:mm}" class="form-control" /&gt;
  &lt;input asp-for="ProjectStart" asp-format="{0:dd.MM.yyyy HH:mm}" class="form-control" /&gt;
  &lt;input asp-for="ProjectStart" asp-format="{0:dd-MM-yyyy HH:mm}" class="form-control" /&gt;
  </code></pre>
  
  <p>Both solutions just cleared out the value in the input field, displaying only "mm/dd/yyyy --:-- --", i.e. the format was not changed, and the data disappeared.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>This is the rendered HTML of the input tag with the <code>asp-format</code>-attribute:</p>
  
  <pre><code>&lt;input class="form-control" 
         type="datetime-local" 
         data-val="true" 
         data-val-required="The Starttidspunkt field is required." 
         id="ProjectStart" 
         name="ProjectStart"
         value="12.10.2019 07:12" /&gt;
  </code></pre>
  
  <p>As you can see, the format is correct, and the data is there. But in my browser, the datepicker displays "mm/dd/yyyy --:-- --".</p>