29

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.

Adi
  • 405
  • 1
  • 7
  • 13
  • Can't you change your EmpData property to string? – LinkedListT Jan 22 '20 at 19:27
  • Use ISO 8601 format: "2019-01-06T17:16:40.000" – Heretic Monkey Jan 22 '20 at 19:29
  • 1
    @LinkedListT Yes, I can. However, there are a lot of other functionalities that depend on DateTime EmpDate. – Adi Jan 22 '20 at 19:29
  • 2
    Just an aside, but, assuming your JSON comes from JavaScript use `Date.toISOString` on the JS side; ASP.NET handles ISO strings fine. – gabriel.hayes Jan 22 '20 at 19:29
  • @user1538301, I am calling this API from PHP. – Adi Jan 22 '20 at 19:31
  • @Adi not a PHP buff but find a way to convert it into an ISO string on the PHP side before sending it to your ASP.NET server – gabriel.hayes Jan 22 '20 at 19:32
  • Instead of "DateTime" class, try using "DateTimeOffset" struct. This works for most JSON APIs I call. – ForbiddenSoul Jan 22 '20 at 19:34
  • 1
    Hello all, ISO 8601 is working for me in Postman. I will update now in code. Thank you all. – Adi Jan 22 '20 at 19:38
  • Aside, @Adi, should that be `public class Employee`? You're binding to an `Employee` object, but your class name is `Lead`. (I assume this is just a typo, but it may be worth editing to avoid confusion.) – Jeremy Caney Jan 22 '20 at 19:59
  • 1
    The date-time value in the json is not wrong. It should be considered as a "custom formatting". Despite ISO 8601 is working, it does not mean that everyone has to use it. String data parsing is a concern of business logic and if I need to use "yyyy-MM-dd hh:mm:ss" formatting then I need to say the parser to use that formatting. Think about this way. You're doing a 3rd party integration and they're not changing their formatting, say they're a payment gateway provider and widely used world-wide. – Hasan Manzak Nov 21 '20 at 21:24

1 Answers1

69

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

2019-01-06T17:16:40

Most parsers use ISO 8601

Sean
  • 1,359
  • 11
  • 15
  • 5
    Actually the date-time value in the json is not wrong. It should be considered as a "custom formatting". Despite of most parsers using ISO 8601 by default, it does not mean that you/him/me have to use it. String data parsing is a concern of business logic and if I need to use "yyyy-MM-dd hh:mm:ss" formatting then I need to say the parser to use that formatting. Think about this way. You're doing a 3rd party integration and they're not changing their formatting, say they're a payment gateway provider and widely used world-wide. – Hasan Manzak Nov 21 '20 at 21:20
  • 2
    Also use `new Date(stringValue).toISOString()` – Uladzislau Ulasenka Dec 02 '20 at 11:16
  • 1
    You might also want to specify a timezone, e.g `2019-01-06T17:16:40Z` (Z at the end means it's UTC) – Bartek Pacia Dec 08 '21 at 10:35