1

In my apicontroller, I have a get method that takes in a complex type ModelA, which consist of multiple fields and one of them is a DateTime.

I am passing in date format as "dd/MM/yyyy" but it is picking up as "MM/dd/yyyy"

[HttpGet]
public async Task<IHttpActionResult> MethodA([FromUri]ModelA model)
{
}

public class ModelA
{
    public DateTime? FromDate {get;set;}
}

How can I have it to pick up the correct format?

What have I tried:

  • overriding ExecuteBindingAsync on HttpParameterBinding and adding parameterBindingRule in HttpConfig
  • overriding BindModel on DefaultModel Binder and adding the binder to ModelBinders

both overridden methods did not get called when I call the api

Mc Kevin
  • 962
  • 10
  • 31
  • It may be due to your current locale/culture settings in your PC. You must either change the setting in your machine or set the preferred locale/culture in code. – Thangadurai May 03 '17 at 03:42
  • I've added into my web.config and have changed the date format in controlpanel/clock to d/M/yyyy but it is still giving me M/d/yyyy in mvc – Mc Kevin May 03 '17 at 05:32
  • How you are checking the date format? Do you see the value of 'FromDate' in debugger or showing that date somewhere in your pages? – Thangadurai May 03 '17 at 06:09
  • Can you add this line in your MethodA and see what is the culture the current thread is set to. Thread.CurrentThread.CurrentCulture.Name – Thangadurai May 03 '17 at 06:13
  • @Thangadurai I put a breakpoint right in the first line of MethodA, I passed in the date as 3/5/2017 but the datetime object is telling me the day is 5 and month is 3 – Mc Kevin May 03 '17 at 06:22
  • Please check what is the culture the current thread is set to. Use this stmt Thread.CurrentThread.CurrentCulture.Name – Thangadurai May 03 '17 at 06:25
  • @Thangadurai it's en-MY – Mc Kevin May 03 '17 at 06:29
  • Possible duplicate of [MVC DateTime binding with incorrect date format](http://stackoverflow.com/questions/528545/mvc-datetime-binding-with-incorrect-date-format) – Thangadurai May 03 '17 at 07:01

0 Answers0