0

I am trying to load today's date into the HTML5 date input type in Core 1.1, however when I set today's date in the controller, I get the following error in the console on the page.

The specified value "9/3/2017 12:00:00 AM" does not conform to the required format, "yyyy-MM-dd".

AddWorkOrder.cshtml

<div class="form-group">
    <b>Issue Date:</b>
    @Html.TextBoxFor(Model => Model.NewWorkOrder.workOrderIssueDate, new { @class = "form-control", @type = "date", @value = Model.NewWorkOrder.workOrderIssueDate.ToString("yyyy-MM-dd") })
    @Html.ValidationMessageFor(Model => Model.NewWorkOrder.workOrderIssueDate, "", new { @class = "valColor" })
</div>

WorkOrderController.cs

// GET : /WorkOrder/AddWorkOrder
public IActionResult AddWorkOrder()
{
    WorkOrderViewModel workOrderVm = new WorkOrderViewModel();
    using (var db = new WorkOrderDBContext())
    {
        workOrderVm.NewWorkOrder = new WorkOrder();
        workOrderVm.NewWorkOrder.workOrderIssueDate = DateTime.Today;
    }
    return View(workOrderVm);
}

I have tried setting the value of the textbook to be @value = DateTime.Today.ToString("yyyy-MM-dd") and that does not load the date either.

2
  • Have you tried to make workOrderIssueDate property as string and convert the DateTime.Today to string like the following workOrderVm.NewWorkOrder.workOrderIssueDate = DateTime.Today.ToString("yyyy-MM-dd");? Commented Sep 3, 2017 at 17:42
  • I have multiple date fields and they are all DateTime's in MSSQL database tables. I would like to keep them DateTime if possible so that all of the fields in my database are not all strings. Commented Sep 3, 2017 at 18:06

2 Answers 2

2

You can use data-format attribute like this for post correct format date:

@Html.TextBoxFor(Model => Model.NewWorkOrder.workOrderIssueDate, "{0:yyyy-MM-dd}", new { @class = "form-control", @type = "date" })
Sign up to request clarification or add additional context in comments.

3 Comments

Tried this, got an error on @data-format = "yyyy-MM-dd" in visual studios saying "The name 'format' does not exist in the current context. Invalid anonymous type member declarator. Anonymous type members must be declared with a member argument, simple name or member access."
Do you know what the format would be for time would be? It is not loading the time and has a similar error of The specified value "14:29:45.9712712" 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. @Html.TextBoxFor(Model => Model.NewWorkOrder.workOrderProcessTime, new { @class = "form-control", @type = "time" })
Figured it out, it's {0:hh\\:mm}
0

I was having a very similar problem but I had a clue because I had two dates on my input form and one was working while the other wasnt. I got very confused because the HTML and the Razor markup was exactly the same. Then I noticed that for the date that wasnt working as expected, I decorated the attribute in the model wtih

[DataType(DataType.Date)]

but for the value that worked correctly I decorated that as

[DataType(DataType.DateTime)]

Once I changed them both to be DateTime, the error went away. I don't pretend to understand why.

Comments

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.