如果有日期和时间,如何显示日期?

时间:2017-08-22 07:34:39

标签: sql asp.net asp.net-mvc-5

显示视图时。它以DateTime格式显示order_dateexpected_date

它将order_date显示为8/19/2017 12:00:00 AM,将expected_date显示为8/22/2017 12:00:00 AM。在SQL Server中,order_dateexpected_date都具有Date数据类型。

我该怎么办才能显示日期?

 <table>
    @{
    int cann = ((DataTable)ViewBag.orderdata).Rows.Count;
    DataTable dt = (DataTable)ViewBag.orderdata;
    for (int i = 0; i < cann; i++)
    {
    <tr>
    <td>@dt.Rows[i]["order_id"]</td>
    <td>@dt.Rows[i]["customer_name"]</td>
    <td>@dt.Rows[i]["Customer_address"]</td>
    <td>@dt.Rows[i]["customer_phone"]</td>
    <td>@dt.Rows[i]["order_date"]</td>
    <td>@dt.Rows[i]["expected_date"]</td>
    <td>@dt.Rows[i]["total_amount"]</td>
    <td>@dt.Rows[i]["advance_amount"]</td>
    <td>@dt.Rows[i]["due_amount"]</td>
    <td><a href="/NewOrder/EditOrder?id=@dt.Rows[i]["order_id"]" 
    class="glyphicon glyphicon-wrench"></a>|<a href="/NewOrder/DeleteOrder?
    id=@dt.Rows[i]["order_id"]" class="glyphicon glyphicon-trash"></a>|<a 
    href="/NewOrder/DetailsData?id=@dt.Rows[i]["order_id"]" class="glyphicon 
    glyphicon-dashboard"></a></td>

    </tr>
    }
    }
    </table>

在控制器中:

 public ActionResult ViewOrder()
        {
            SqlQuery.Order see = new SqlQuery.Order();
            DataTable view = see.ViewOrder();
            ViewBag.orderdata = view;
            return View();
        }`

Sql Query:

 public DataTable ViewOrder()
    {
        string sql = "select * from ProductOrder";
        con.Open();
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataAdapter view = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        view.Fill(dt);
        con.Close();
        return dt;
    }

5 个答案:

答案 0 :(得分:2)

老兄,首先,这不是一个很好的做法,在你看来你的身份,请阅读分层架构。了解数据层责任。 但你的问题的答案是

@DateTime.Parse(dt.Rows[i]["order_date"]).ToString("yy/MM/dd");

答案 1 :(得分:1)

{{1}}

答案 2 :(得分:1)

您可以使用CAST功能将日期时间转换为日期,如下所示:

sql = "select order_id, customer_name, Customer_address, customer_phone,
CAST(order_date as date) as order_date, CAST(expected_date as date) as expected_date,
total_amount, advance_amount, due_amount from ProductOrder"

答案 3 :(得分:1)

SQL Server:

select CONVERT(VARCHAR(12), expected_date, 103) AS expected_date,  CONVERT(VARCHAR(12), order_date, 103) AS order_date from ProductOrder

答案 4 :(得分:0)

@ dt.Rows [I] [&#34; order_date的&#34]。Date.ToString(&#34; d&#34)