【发布时间】:2014-01-21 12:31:54
【问题描述】:
我在 asp.net 中使用 datetime 时遇到了一个问题。根据土耳其文化将日期时间字符串从文本框发送到 DateTime 时,datetime 格式被破坏。
例如;我在查询字符串上发送这些参数 -
?beginDate=02.01.2014&endDate=03.01.2014,这些参数值在传递ActionResult参数时如下所示-
beginDate = 01.02.2014, endDate = 03.01.2014.
我尝试了几个解决方案,但问题仍然存在。
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")
网址
domain.com/Home/GetList?begin=02.01.2014&end=03.01.2014
GetList 方法
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")))
}
【问题讨论】:
-
请显示您将这些查询字符串参数转换回日期时间的代码。
-
查询字符串参数直接绑定
标签: c# asp.net-mvc datetime cultureinfo culture