Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to acccept date in dd-MM-yyyy format in net core web api?

I am using net core 3.1 in my web API project. I have created one API which accepts the date from the user. By default MM-dd-yyyy format is accepted in the project. But I want to accept the date in dd-MM-yyyy format and validate all dates accordingly.

Below is my api :

    [HttpGet]
    public async Task<IActionResult> Get(DateTime fromDate, DateTime toDate)
    {
        return Ok();
    }

Also, I have APIs in which the date parameter is passed in the request body as JSON. I have tried following StackOverflow answers but nothing worked:

https://stackoverflow.com/a/58103218/11742476

The above solution worked when passing the date inside the request body but not when passing the date in the URL.

Is there any other way through which I can achieve this. ?

like image 983
Sunny Avatar asked Jan 27 '26 13:01

Sunny


1 Answers

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:

enter image description here

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

like image 69
Rena Avatar answered Jan 29 '26 04:01

Rena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!