2

I am receiving POST data from a vendor's web service in my web api. One of the values being sent is a data and time - sample: StartTime=2014-10-20+15%3A30%3A54. I understand that %3A is an escape for :, which would have a date in the following format: 2014-10-20+15:30:54.

The problem is that the StartTime value is not being parsed properly by the web api, and all the StartDate properties on my model are being set to 1/1/0001 12:00:00 AM.

Can anyone point me in the right direction here? I can't seem to find any info on what format this date is in to see if there is a way to have the web api framework parse it correctly.

ChandlerPelhams
  • 1,648
  • 4
  • 38
  • 56
  • I had this before Web API expects date time in the form of MM-dd-yyyy. You can try formatting it this way ... – Milen Oct 21 '14 at 14:07
  • The problem is that I am getting the information passed from a 3rd party. I do not have any control over the format of the date they are passing. – ChandlerPelhams Oct 21 '14 at 14:09
  • Can you do `var result = await response.Content.ReadAsAsync` where is your response object ? – Milen Oct 21 '14 at 14:19
  • JSON.NET supports custom DateTime converters. You can take a look at this question: http://stackoverflow.com/questions/12936614/asp-net-web-api-date-format-in-json-does-not-serialise-successfully – Nikolai Samteladze Oct 21 '14 at 14:24

1 Answers1

0

Try this

Request Body:

{"StartTime":"2014-10-20+15:30:54"}

Parse:

CultureInfo provider = CultureInfo.InvariantCulture;
string format = "yyyy-MM-dd+HH:mm:ss";
DateTime dt = DateTime.ParseExact(request.StartTime, format, provider);
Melih Mucuk
  • 6,988
  • 6
  • 37
  • 56