如何修复下面的代码以返回带秒的日期对象?执行tostring()
只需返回分钟的日期和时间。
我用刻度线尝试了它,它也不受支持。此函数返回视图下拉列表的datetime
字符串,以便用户可以选择日期时间字符串来过滤搜索功能。没有日期时间的秒数,搜索功能无法正常工作。
如果还有另一种方式使用日期进行过滤搜索,请告诉我。但是,我认为如果我可以让它以秒返回它;它会工作正常。
//gets filtered list with a group by on date,
//just want to grab unique dates by the two filtered ids
IQueryable<mylistobject> filteredlist =
from c in db.mylistcontext
where(c.TypeId == TypeId && c.DeptId == deptid)
group c by new { c.myDateTime } into grp
select grp.FirstOrDefault();
//form the value id / name
var result = (
from r in filteredlist
select new {
//id = r.myDateTime.Ticks.ToString(), //ticks is not supported
id = r.myDateTime.ToString(), //the seconds is truncated
name = r.myDateTime.ToString() // the seconds gets cut off
}).ToList();
return Json(result, JsonRequestBehavior.AllowGet);
答案 0 :(得分:0)
您可以在不转换的情况下传递日期
1)ASP.NET MVC GET操作 - 返回日期
[HttpGet]
public JsonResult GetDates()
{
var result = new List<DateTime> {new DateTime(2013, 1, 1), new DateTime(2013, 4, 4)}
.Select(x => new {id = x, name = x.ToString(CultureInfo.InvariantCulture)});
return Json(result, JsonRequestBehavior.AllowGet);
}
2)客户端上的Сode从服务器接收日期并发送“id”(这是我们的C#DateTime)而不更改
$.get("home/GetDates", function(dates) {
console.log(dates);
$.post("home/ReceiveDate", { date: dates[0].id }, function(postResult) {
console.log(postResult);
}, "json");
});
3)ASP.NET MVC POST操作 - 您可以接受没有转换的JSON日期
[HttpPost]
public JsonResult ReceiveDate(DateTime date)
{
return Json(new { date }, JsonRequestBehavior.AllowGet);
}