You could custom model binder for the DateTime format like below:
1.DateTimeModelBinderProvider:
public class DateTimeModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (DateTimeModelBinder.SUPPORTED_TYPES.Contains(context.Metadata.ModelType))
{
return new BinderTypeModelBinder(typeof(DateTimeModelBinder));
}
return null;
}
}
2.DateTimeModelBinder:
public class DateTimeModelBinder : IModelBinder
{
public static readonly Type[] SUPPORTED_TYPES = new Type[] { typeof(DateTime), typeof(DateTime?) };
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
if (!SUPPORTED_TYPES.Contains(bindingContext.ModelType))
{
return Task.CompletedTask;
}
var modelName = GetModelName(bindingContext);
var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);
if (valueProviderResult == ValueProviderResult.None)
{
return Task.CompletedTask;
}
bindingContext.ModelState.SetModelValue(modelName, valueProviderResult);
var dateToParse = valueProviderResult.FirstValue;
if (string.IsNullOrEmpty(dateToParse))
{
return Task.CompletedTask;
}
var dateTime = Helper.ParseDateTime(dateToParse);
bindingContext.Result = ModelBindingResult.Success(dateTime);
return Task.CompletedTask;
}
private string GetModelName(ModelBindingContext bindingContext)
{
if (!string.IsNullOrEmpty(bindingContext.BinderModelName))
{
return bindingContext.BinderModelName;
}
return bindingContext.ModelName;
}
}
public class Helper
{
public static DateTime? ParseDateTime(
string dateToParse,
string[] formats = null,
IFormatProvider provider = null,
DateTimeStyles styles = DateTimeStyles.None)
{
var CUSTOM_DATE_FORMATS = new string[]
{
//"MM-dd-yyyy",
"yyyy-MM-dd",
"dd-MM-yyyy"
};
if (formats == null || !formats.Any())
{
formats = CUSTOM_DATE_FORMATS;
}
DateTime validDate;
foreach (var format in formats)
{
if (format.EndsWith("Z"))
{
if (DateTime.TryParseExact(dateToParse, format,
provider,
DateTimeStyles.AssumeUniversal,
out validDate))
{
return validDate;
}
}
if (DateTime.TryParseExact(dateToParse, format,
provider, styles, out validDate))
{
return validDate;
}
}
return null;
}
}
3.Startup.cs:
services.AddControllers(option =>
{
// add the custom binder at the top of the collection
option.ModelBinderProviders.Insert(0, new DateTimeModelBinderProvider());
})
If you still want to display the dd-MM-yyyy
format date,change your Startup.cs:
services.AddControllers(option =>
{
option.ModelBinderProviders.Insert(0, new DateTimeModelBinderProvider());
}).AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
});
Result:

Reference:
http://www.vickram.me/custom-datetime-model-binding-in-asp-net-core-web-api
Update:
You could see that you can pass dd-MM-yyyy
date to the action but the receive format is still as before.This is by design,refer to:
https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-3.1#globalization-behavior-of-model-binding-route-data-and-query-strings