The JSON value could not be converted to System.DateTime

Adi picture Adi · Jan 22, 2020 · Viewed 12.1k times · Source

I have an Employee table

public class Employee
{
   [Key]
   public long ID { get; set; }
   public DateTime EmpDate { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
}

I have created web API to post the employee data:

[HttpPost]
public async Task<ActionResult<Employee>> PostLead(Employee employee)
{
      //Code to post
}

This is my JSON body

{
    "firstname":"xyz",
    "lastname":"abc",
    "EmpDate":"2019-01-06 17:16:40"
}

I am getting error as The JSON value could not be converted to System.DateTime. But when I pass EmpDate value as 2019-01-06, I am not getting an error.

Answer

Sean picture Sean · Jan 22, 2020

your date value in your JSON isn't correct. should be

2019-01-06T17:16:40

Most parsers use ISO 8601