1

我的时间字符串有问题..我想格式化它,以便它显示更整洁的设计..谁能帮我解决这个问题

这是我的代码:

 ViewBag.startTime = 
 (from a in test
  where a.ID == clientCustomerPositionShiftnfo.ID
  select new{ a.StartTime})
  .AsEnumerable()
  .Select(a => a.StartTime != "Anytime"
  ? Convert.ToDateTime(a.StartTime).ToString("HH:mm:ss")
  : a.StartTime.Trim());

在我看来:

    <input type="text" id="txtStartTime" name="txtStartTime" class="inputLong"
value="@ViewBag.startTime" disabled="disabled"/>
4

1 回答 1

3

First在查询后尝试调用:

ViewBag.startTime = 
    (from a in test
     where a.ID == clientCustomerPositionShiftnfo.ID
     select a.StartTime)
    .AsEnumerable()
    .Select(t => t != "Anytime" ? Convert.ToDateTime(t).ToString("HH:mm:ss") : t)
    .First(); // or FirstOrDefault if your query might not return any results

或者也许更干净:

var startTime = 
    (from a in test
     where a.ID == clientCustomerPositionShiftnfo.ID
     select a.StartTime)
    .First(); // or FirstOrDefault if your query might not return any results
ViewBag.startTime startTime != "Anytime" 
    ? Convert.ToDateTime(startTime).ToString("HH:mm:ss") 
    : startTime;
于 2013-09-09T02:51:42.350 回答