3

I'm using datetime in Route attribute like that:

[Route("{givenDate:datetime}")]

but it's in "American" format which is "month-day-year".

How can I convert it to "day-month-year" format?

PS. I'm aware that I can use other formats like "yearmonthday" or "year-month-day", but "day-month-year" looks more intuitive for me.

4
  • Possible duplicate of stackoverflow.com/questions/919244/… question. Commented Oct 24, 2018 at 7:40
  • 2
    I don't think so. I know to change DateTime formats, but i don't know how to set this in Route attribute :) Commented Oct 24, 2018 at 7:44
  • 2
    It doesn't look like you can change the datetime constraint: See the docs, notably the warning at the bottom of the linked section. Commented Oct 24, 2018 at 7:47
  • Ohh... That's a shame I guess. But thank you Commented Oct 24, 2018 at 7:57

1 Answer 1

3

You could instead use a route like:

[Route("{day:regex(^[[0-2]][[0-9]]|3[[0-1]]$)}-{month:regex(^0[[0-9]]|1[[0-2]]$)}-{year:regex(^\\d{{4}}$)}")]

And then change your action to something like:

public IActionResult Foo(int day, int month, int year)
{
    var givenDate = new DateTime(year, month, day);

    ...
}

Admittedly, that kind of sucks, but it does get the job done. The regex constraints, while ugly as hell, will ensure that ultimately any values that get through will work to construct a DateTime object with.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.