0

The following javascript textual date will not bind to C# DateTime:

Tue Jun 4 2019 17:00:00 GMT-0500 (Central Daylight Time)

[HttpGet, Route("Events")]
[Authorize(Permissions.EventRead)]
public async Task<IHttpActionResult> GetEvents(DateTime? date = null)

The date parameter will be null, however when formatted as 'm/d/yyyy hh:mm:ss' then the date will bind as expected.

Why doesn't this happen out of the box? Is there some middleware configuration to support binding javascript textual dates?

Igor
  • 60,821
  • 10
  • 100
  • 175
Breakskater
  • 415
  • 1
  • 4
  • 18
  • 1
    Recommended reading: https://stackoverflow.com/questions/10286204/the-right-json-date-format –  Jun 04 '19 at 19:12
  • Nice, I am bookmarking that link @Amy. – Igor Jun 04 '19 at 19:13
  • 2
    *Why doesn't the .NET Framework bind javascript textual dates to C# DateTime?* I can only guess that it doesn't make sense to do it that way because beyond the fact that there are standards across languages and operating system about how Dates and/or Times should be handled, typically data should be passed with the smallest amount without the loss of precision. Adding the Day (Tue) and the name of the Offset (Central Daylight Time) add no benefit. – Erik Philips Jun 04 '19 at 19:15
  • The textual format is the default so either javascript isn't conforming to standards or .NET doesn't bind to one of the standard formats. Which one is it? As a client consuming a .NET endpoint, I would expect it to take javascript's default date format without needing to convert it first to something .NET will accept – Breakskater Jun 07 '19 at 19:22

0 Answers0