0

I have encounter an issue while working with datetime in asp.net. While sending datetime string from an textbox to DateTime according to the Turkish Culture, the datetime format is breaking.

For instance; i am sending these parameters on querystring -

?beginDate=02.01.2014&endDate=03.01.2014, these parameters values appear like the following when passed ActionResult parameter -

beginDate = 01.02.2014, endDate = 03.01.2014.

I tried a couple solution but problems still exists.

1)

protected void Application_Start()
{
    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("tr-TR");
    System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("tr-TR");

}

2)

<globalization uiCulture="tr-TR" culture="tr-TR" />

3)

return Json(_db.GetList(Convert.ToDateTime(begin, new CultureInfo("tr-TR")), Convert.ToDateTime(end, new CultureInfo("tr-TR")))

4)

begin.Value.ToString("dd.mm.yyyy")
end.Value.ToString("dd.mm.yyyy")

Url

domain.com/Home/GetList?begin=02.01.2014&end=03.01.2014

GetList Method

public JsonResult GetList(DateTime? begin, DateTime? end)
{
    return Json(_db.GetList(Convert.ToDateTime(begin, new CultureInfo("tr-TR")), Convert.ToDateTime(end, new CultureInfo("tr-TR")))
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
Goran Zooferic
  • 371
  • 8
  • 23

1 Answers1

0

The default model binder is culture agnostic for GET parameters. This means that you should always use the yyyy-MM-dd following format when you are sending dates as query string parameters to a controller action:

?beginDate=2014-01-02&endDate=2014-01-03

The culture comes into play if you were sending those dates in the payload of a POST request.

And there's a reason behind this implementation details. You could read more about it in the following article: http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx

This being said you could get rid of all the 1., 2., 3., 4., 5. points you tried and simply use the begin and end DateTime parameters. You don't need to be setting the culture info of your application.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928