我的日期字段也显示时间。如何删除时间?

时间:2014-02-01 15:40:52

标签: c# date

我的日期数据类型是日期。并且它显示的时间也是凌晨12:00:00。但在我的数据库中它只有6/6/2014和2016年12月12日,并且没有这个12:00:00 AM。如何删除?以下是我的预约表格代码。请告诉我如何删除时间?

enter image description here

现在有2个日期

enter image description here

enter image description here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class member_viewappointment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            // call BindGridView
            bindGridView();

        }
    }

    private void bindGridView()
    {
        int ID = Convert.ToInt32(Session["ID"].ToString());
        //get connection string from web.config
        string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT pat.pFirstName AS FirstName, aStatus AS Status,  aDate AS Date, aTime AS Time, aContact AS Contact, aHeight AS Height, aWeight AS Weight, med.mcCentre AS MedicalCentre from appointment AS app ";
        strCommandText += " LEFT OUTER JOIN MEDICALCENTRE as med on app.mcid = med.mcid";
        strCommandText += " LEFT OUTER JOIN PATIENT as pat on app.patientid = pat.patientid ";
        strCommandText += " WHERE app.patientid = " + ID.ToString();

        try
        {
            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

            myConnect.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(reader);
            grdViewAppointment.DataSource = dt;
            grdViewAppointment.DataBind();
            lblResult.Text = "";

            reader.Close();
        }
        catch (SqlException ex)
        {
            lblResult.Text = "Error:" + ex.Message.ToString();
        }
        finally
        {
            myConnect.Close();
        }

    }
}

3 个答案:

答案 0 :(得分:2)

使用DateTime时,您可以执行.ToString("d/M/yyyy")

您可以在sql convert(varchar, aDate, 103) AS Date

中执行此操作

答案 1 :(得分:1)

您可以尝试DataFormatString在绑定列中设置日期时间的格式

<asp:BoundField DataField="MyDataField" HeaderText="Date" DataFormatString="{0:MM/dd/yyyy}"  />

答案 2 :(得分:1)

只需运行此查询

private void bindGridView()
    {
        int ID = Convert.ToInt32(Session["ID"].ToString());
        //get connection string from web.config
        string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

 string strCommandText = "SELECT pat.pFirstName AS FirstName, aStatus AS Status,  COnvert(VARCHAR(15),aDate,103) AS Date, aTime AS Time, aContact AS Contact, aHeight AS Height, aWeight AS Weight, med.mcCentre AS MedicalCentre from appointment AS app ";
    strCommandText += " LEFT OUTER JOIN MEDICALCENTRE as med on app.mcid = med.mcid";
    strCommandText += " LEFT OUTER JOIN PATIENT as pat on app.patientid = pat.patientid ";
    strCommandText += " WHERE app.patientid = " + ID.ToString();

        try
        {
            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

            myConnect.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(reader);
            grdViewAppointment.DataSource = dt;
            grdViewAppointment.DataBind();
            lblResult.Text = "";

            reader.Close();
        }
        catch (SqlException ex)
        {
            lblResult.Text = "Error:" + ex.Message.ToString();
        }
        finally
        {
            myConnect.Close();
        }

    }
}