我真的很生气怎么可能c#没有简单的日期格式而没有烦人的小时后
这是我的问题:
我有一个班级
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;
}
然后我只返回一个带有返回列表的GridView 这是我的Page_Load
Dates date = new Dates();
List<Dates> listDate = new List<Dates>();
listDate = GetDates();
gvDates.DataSource = listDate;
gvDates.DataBind
现在有人可以解释为什么当你看到我只得到日期部分
时我会继续获得小时部分答案 0 :(得分:2)
您需要格式化日期,例如
somedate.ToString("d")
还有其他一些日期格式
答案 1 :(得分:0)
如果您正在使用绑定字段,则需要执行以下操作:
<asp:boundfield datafield="Date_Column" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />
你可以change the date format而不喜欢