Home > Enterprise >  Passing date from controller to html time
Passing date from controller to html time

Time:12-18

I am trying to pass date from a controller to a razor view input time element but its not populating the element with hour min and am/pm. In dev tools, I get

The specified value 12/16/2021 12:30:00 AM does not conform to the required format. The format is HH:mm, HH:mm:ss or HH:mm:ss.SSS where HH is 00-23, mm is 00-59, ss is 00-59, and SSS is 000-999.

Can you please tell me what do I need to do to change the format?

 @Html.TextBoxFor(model => model.StartTime, new { @class = "form-control", type="time" }) 

enter image description here

CodePudding user response:

Consider that you're working with what is essentially a textbox. I'd recommend some backend processing:

// 12/16/2021 12:30:00 AM
DateTime time = new DateTime(2021, 12, 16, 0, 30, 0);

// You might want to put this in a new property or change StartTime to a string.
model.StartTime = time.ToString("HH:mm:ss");
  • Related