How to compare two DateTimes on Time only ignoring the date?
Seems like everyone always ignores the time part, but how would you compare two datetimes ignoring the date? if we just compare them as TIME it seems to still favor the oldest date.
(12/02/2004 9:00) > (12/02/2011 8:24) --this would be true.
The below code works but it feels a bit a bit beating around the bush comparing the hours and minutes separately.
var results = from x in dataContext.GetTable<ScheduleEntity>()
where x.LastRunDate < date.Date
&& x.reportingTime.Hour <= date.Hour
&& x.reportingTime.Minute <= date.开发者_如何学编程Minute
&& x.reportingFequency.Substring(position, 1) == scheduled
select x;
Also, the reason we are doing this is because we couldn't get our SQL TIME to compare to a TIMESPAN this says it would be the same but LINQ is returning a "TIME to bigint conversion error".
DateTime
has a TimeOfDay
property, you can use this to compare the times for two dates as follows:
var one = DateTime.Now.AddHours(1);
var two = DateTime.Now;
var diff = one.TimeOfDay - two.TimeOfDay;
精彩评论