[英]How do I compare datetime with value from datepicker?
I need to compare the chosen date with the date in a database and return values within the date range but keep getting incorrect format. 我需要将选择的日期与数据库中的日期进行比较,并返回日期范围内的值,但格式仍然不正确。 The date format in the database is 2016-01-02 00:00:00.000 (from one of the rows)
数据库中的日期格式为2016-01-02 00:00:00.000(来自其中一行)
I am using the code below but am unable to compare and keep getting"String was not recognized as a valid DateTime." 我正在使用下面的代码,但无法比较并保持“字符串未被识别为有效的DateTime”。 Help?
救命?
protected void RadPushButton1_Click(object sender, EventArgs e)
{
DateTime startDate = DateTime.Parse(RadDatePickerStart.SelectedDate.ToString());
string s = startDate.Date.ToString("yyyy-dd-MM");
startDate = DateTime.Parse(s+"12:00:00 AM");
// DateTime endDate = DateTime.Parse(RadDatePickerEnd.SelectedDate.ToString());
EMCSettlementManager man = new EMCSettlementManager();
Grid.DataSource = man.GetSTL010(r => EntityFunctions.TruncateTime( r.settlement_date) >= startDate);
Grid.DataBind();
}
If you want to compare dates, make sure all values are of type DateTime
, not String
. 如果要比较日期,请确保所有值均为
DateTime
类型,而不是String
类型。
Try something like this: 尝试这样的事情:
DateTime startDate = RadDatePickerStart.SelectedDate;
EMCSettlementManager man = new EMCSettlementManager();
Grid.DataSource = man.GetSTL010(r => r.settlement_date >= startDate);
If you want to get rid of the time-part, you could do something like this: 如果要摆脱时间限制,可以执行以下操作:
DateTime startDate = RadDatePickerStart.SelectedDate.Date;
If the field in the database is a string-type, not a datetime, please convert it to a Date(Time)-type. 如果数据库中的字段是字符串类型,而不是日期时间,请将其转换为日期(时间)类型。 Using strings everywhere is a bad choise, alsnog known as 'stringly typed'.
到处都使用字符串是一个不好的选择,也被称为“字符串类型”。 (See: https://blog.codinghorror.com/new-programming-jargon/ )
(请参阅: https : //blog.codinghorror.com/new-programming-jargon/ )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.