2013-10-10 47 views
0

只得到日期是我的问题:不能从日期时间

我已经得到了一类

public class Dates 
    { 
     // private int excursionID; 
     private int excursion_date_ID; 
     private DateTime startdates; 
     private double prices; 

     /* public int ExcursionID 
     { 
      get { return excursionID; } 
      set { excursionID = value; } 

     }*/ 
     public int Excursion_date_ID 
     { 
      get { return excursion_date_ID; } 
      set { excursion_date_ID = value; } 
     } 
     public DateTime StartDates 
     { 
      get { return startdates.Date; } 
      set { startdates = value.Date; } 
     } 

     public double Prices 
     { 
      get { return prices; } 
      set { prices = value; } 
     } 

     public Dates() { } 
     } 
    } 

然后在pageLoad的我从数据库中获取一些数据

public List<Dates> GetDates() 
     { 
      List<Dates> datesList = new List<Dates>(); 

      string connectionString = "Server=localhost\\SQLEXPRESS;Database=excursion;Trusted_Connection=true"; 
      string excursionnID = Request.QueryString["ExcursionID"]; 
      string query = 
     "SELECT Excursion_date_ID, Start_date, Price FROM EXCURSION_DATES WHERE EXCURSION_ID='" + excursionnID + "'"; 
      SqlConnection conn = new SqlConnection(connectionString); 
      SqlCommand cmd = new SqlCommand(query, conn); 
      try 
      { 
       conn.Open(); 
       SqlDataReader rd = cmd.ExecuteReader(); 
       int s=0; 
       while (rd.Read()) 
       { 
        Dates dates = new Dates(); 

        // dates.ExcursionID = Convert.ToInt32(rd["Excursion_ID"]); 
        dates.Excursion_date_ID=Convert.ToInt32(rd["Excursion_date_ID"]); 
        DateTime excursiondatetime = (DateTime)rd["Start_date"]; 
        dates.StartDates = excursiondatetime.Date; 
        dates.Prices = Convert.ToDouble(rd["Price"]); 
        datesList.Add(dates); 

       } 
      } 
      catch (Exception EX) 
      { 

      } 
      return datesList; 
     } 

然后我只RET瓮填写一个GridView控件与returnes名单 这里是我的Page_Load

Dates date = new Dates(); 
      List<Dates> listDate = new List<Dates>(); 
      listDate = GetDates(); 
      gvDates.DataSource = listDate; 
      gvDates.DataBind 

现在有人可以解释为什么我不断收到小时部分,当你看到,我只得到了日期部分

+5

'Date'仍然返回一个'DateTime',其中包括良好,日期和时间。如果您只想显示需要字符串格式的日期,请执行以下操作:http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx –

+1

是否显示00:00:00的日期和时间?如果是这样,如果你在gridview中这样做,你可能想要改变dusplay格式,只有 –

回答

2

你需要太格式化日期像

somedate.ToString("d") 

也有提供

+1

,排序列将按字母顺序排序,而不是按日期值排序。 –

0

如果您使用绑定领域的一些其他的DateFormats,你需要这样做:

<asp:boundfield datafield="Date_Column" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" /> 

您可以change the date format但是你喜欢