Incorrect Date Formatting in .NET 6 with DateOnly Type

I’m facing an issue with the DateOnly type in a .NET 6 API where the date format is not mapped correctly when passed via a query parameter. The expected date format is dd.MM.yyyy, which is standard for Norway, but it’s being interpreted as MM.dd.yyyy instead.

Here’s the relevant part of my controller:

[HttpGet("data")]
public Task<IActionResult> GetData([FromQuery] DateOnly date)
{
    return Task.FromResult<IActionResult>(Ok($"Day {date.Day} Month {date.Month} Year {date.Year}"));
}

And here’s an example request:

http://localhost:7053/data?date=01.04.2024

However the output is:

Day 04 Month 01 Year 2024

This indicates that the date is being interpreted as Jauary 4th, 2024, rather than April 1st, 2024. The application’s culture is set to Norway, where the standard date format is dd.MM.yyyy. I’m looking for a solution that specifically works with the DateOnly type, without resorting to using DateTime. I am aware that DateTime could be used as a workaround, but I need a solution that adheres to using DateOnly.

Has anyone encountered this issue or know of a way to ensure the correct date format is used with DateOnly in .NET 6?

To ensure that the DateOnly parameter in your C# controller receives dates in the dd.MM.yyyy format when sent from the client, you need to configure model binding in your ASP.NET Core application. By default, ASP.NET Core interprets dates based on the culture settings of the server, which might be causing the mix-up between day and month in your scenario.

Here’s how you can ensure the DateOnly parameter accepts dates in the specified format. You will need to create a custom model binder that explicitly parses dates using the dd.MM.yyyy format.

public class DateOnlyModelBinder : IModelBinder
{
  public Task BindModelAsync(ModelBindingContext bindingContext)
  {
    if (bindingContext == null)
    {
        throw new ArgumentNullException(nameof(bindingContext));
    }

    var modelName = bindingContext.ModelName;
    var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);

    if (valueProviderResult == ValueProviderResult.None)
    {
        return Task.CompletedTask;
    }

    bindingContext.ModelState.SetModelValue(modelName, valueProviderResult);

    var dateValue = valueProviderResult.FirstValue;

    if (string.IsNullOrEmpty(dateValue))
    {
        return Task.CompletedTask;
    }

    if (DateOnly.TryParseExact(dateValue, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
    {
        bindingContext.Result = ModelBindingResult.Success(date);
    }
    else
    {
        bindingContext.ModelState.TryAddModelError(modelName, "Date must be in dd.MM.yyyy format.");
    }

    return Task.CompletedTask;
  }
}

After creating the custom model binder, you need to inform ASP.NET Core to use this binder for DateOnly types. You can do this by creating a model binder provider and adding it to the MVC options in your startup configuration.

public class DateOnlyModelBinderProvider : IModelBinderProvider
{
  public IModelBinder GetBinder(ModelBinderProviderContext context)
  {
    if (context == null)
    {
        throw new ArgumentNullException(nameof(context));
    }

    if (context.Metadata.ModelType == typeof(DateOnly))
    {
        return new DateOnlyModelBinder();
    }

    return null;
  }
}

In your dependency injection file you must use the binder provider like so:

public void ConfigureServices(IServiceCollection services)
{
  services.AddControllers(options =>
  {
    // Add the custom model binder at the beginning of the collection
    options.ModelBinderProviders.Insert(0, new DateOnlyModelBinderProvider());
  });
}

By implementing a custom model binder like this, you explicitly control the date format handling and prevent common issues related to regional and cultural settings on different servers and clients.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật