user2270653
user2270653

Reputation: 247

Date format changing passing date from view to controller

When passing a date from a MVC view, to a GET action in the controller the date value changes from 01/03/2018 00:00:00 to: 03/01/2018 00:00:00 I have attempted to format it using @Model.CalendarOptions.DefaultDate.ToString("dd/MM/yyyy") but it behaves the same. Any ideas?

<a class="modal-link btn btn-default pull-right" id="calendar-booking-button" data-target="modal-container" data-toggle="modal" asp-action="CreateModal" asp-controller="Bookings" asp-route-bookingStartDate="@Model.CalendarOptions.DefaultDate.ToString("dd/MM/yyyy")" asp-route-staffid="@staff.StaffId"> `

I saw a post advising to add localization in the startup however that has not helped.

      services.Configure<RequestLocalizationOptions>(options =>
        {
            options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-GB");
            options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-GB") };
            options.RequestCultureProviders.Clear();
        });

Upvotes: 0

Views: 1057

Answers (1)

Georg Patscheider
Georg Patscheider

Reputation: 9463

Use the "Round-trip" format to pass the date (DateTime.ToString("o")). This is an unambiguous ISO 8601 format.

<a asp-route-bookingStartDate="@Model.CalendarOptions.DefaultDate.ToString("o")">

How to: Round-trip Date and Time Values

The Round-trip ("O", "o") Format Specifier

Upvotes: 1

Related Questions