I am using webapi2. I have a property in model is start date whose datatype is datetime. I want to pass the date as "dd-mm-yyyy" format. But if i send, i am getting 400 bad request. Could you please help me out. Note, I am using Fluent validation for the model validation.
public class Item
{
public DateTime? StartDate { get; set; }
public string Id { get; set; }
}
You have 3 options.
Option
ISO8601
Don't pass it as "dd-mm-yyyy". Pass it instead in ISO8601 format (
yyyy-MM-dd
). That is the correct way to serializeDateTime
s to string and also for then communicating that string representation between tiers. This format is a standard, widely used, unambiguous, and almost all frameworks that I am aware of have built in mechanisms for outputtingDateTime
s to that format and parsing them from that format.Displaying a
DateTime
formatted as "dd-mm-yyyy" is a presentation layer concern and it should stay there and not "bleed" into the other application layers.Option Formatters
Use custom code, like a Json Converte or an
ActionFilterAttribute
, to read the incomingDateTime
.Option String
Accept a string parameter instead and handle your own parsing inside the controller's method.
I honestly do not recommend the last 2 options. Instead use ISO8601: a standard, unambiguous, widely accepted means of communicating a
DateTime
.