Answers:
尝试Date
在DateTime
对象上使用属性...
if(dtOne.Date == dtTwo.Date)
....
为了进行真正的比较,您可以使用:
dateTime1.Date.CompareTo(dateTime2.Date);
-1
,0
而且1
,真的吗?它们只是代表“更少”,“相等”和“更大”的神奇数字。然后,您将必须将所得的整数“比较”为某些东西,因为存在三个可能的值。我必须用@大卫同意,这是更自然的使用dateTime1.Date < dateTime1.Date
,同样有<=
,>
而且>=
,在大多数应用中。
这就是我要与LINQ一起使用的方式。
DateTime date_time_to_compare = DateTime.Now;
//Compare only date parts
context.YourObject.FirstOrDefault(r =>
EntityFunctions.TruncateTime(r.date) == EntityFunctions.TruncateTime(date_to_compare));
如果仅使用dtOne.Date == dtTwo.Date
它,则无法与LINQ一起使用(错误:LINQ to Entities不支持指定的类型成员'Date')
EntityFunctions
在.NET 4.5.2中已弃用。改用它:DbFunctions.TruncateTime
。这似乎是相同的方法,只是移动..
如果您使用的是Entity Framework <v6.0,请使用EntityFunctions.TruncateTime
如果您使用的是Entity Framework> = v6.0,请使用DbFunctions.TruncateTime
围绕任何一个使用(根据您的EF版本) DateTime
您要在Linq查询中使用的类属性
例
var list = db.Cars.Where(c=> DbFunctions.TruncateTime(c.CreatedDate)
>= DbFunctions.TruncateTime(DateTime.UtcNow));
DateTime dt1=DateTime.ParseExact(date1,"dd-MM-yyyy",null);
DateTime dt2=DateTime.ParseExact(date2,"dd-MM-yyyy",null);
int cmp=dt1.CompareTo(dt2);
if(cmp>0) {
// date1 is greater means date1 is comes after date2
} else if(cmp<0) {
// date2 is greater means date1 is comes after date1
} else {
// date1 is same as date2
}
您可以使用Equals或CompareTo。
等于:返回一个值,该值指示两个DateTime实例是否具有相同的日期和时间值。
CompareTo返回值:
DateTime可为空:
DateTime? first = new DateTime(1992,02,02,20,50,1);
DateTime? second = new DateTime(1992, 02, 02, 20, 50, 2);
if (first.Value.Date.Equals(second.Value.Date))
{
Console.WriteLine("Equal");
}
要么
DateTime? first = new DateTime(1992,02,02,20,50,1);
DateTime? second = new DateTime(1992, 02, 02, 20, 50, 2);
var compare = first.Value.Date.CompareTo(second.Value.Date);
switch (compare)
{
case 1:
Console.WriteLine("this instance is later than value.");
break;
case 0:
Console.WriteLine("this instance is the same as value.");
break;
default:
Console.WriteLine("this instance is earlier than value.");
break;
}
DateTime不可为空:
DateTime first = new DateTime(1992,02,02,20,50,1);
DateTime second = new DateTime(1992, 02, 02, 20, 50, 2);
if (first.Date.Equals(second.Date))
{
Console.WriteLine("Equal");
}
要么
DateTime first = new DateTime(1992,02,02,20,50,1);
DateTime second = new DateTime(1992, 02, 02, 20, 50, 2);
var compare = first.Date.CompareTo(second.Date);
switch (compare)
{
case 1:
Console.WriteLine("this instance is later than value.");
break;
case 0:
Console.WriteLine("this instance is the same as value.");
break;
default:
Console.WriteLine("this instance is earlier than value.");
break;
}
int o1 = date1.IndexOf("-");
int o2 = date1.IndexOf("-",o1 + 1);
string str11 = date1.Substring(0,o1);
string str12 = date1.Substring(o1 + 1, o2 - o1 - 1);
string str13 = date1.Substring(o2 + 1);
int o21 = date2.IndexOf("-");
int o22 = date2.IndexOf("-", o1 + 1);
string str21 = date2.Substring(0, o1);
string str22 = date2.Substring(o1 + 1, o2 - o1 - 1);
string str23 = date2.Substring(o2 + 1);
if (Convert.ToInt32(str11) > Convert.ToInt32(str21))
{
}
else if (Convert.ToInt32(str12) > Convert.ToInt32(str22))
{
}
else if (Convert.ToInt32(str12) == Convert.ToInt32(str22) && Convert.ToInt32(str13) > Convert.ToInt32(str23))
{
}