C#,如何仅从日期时间检查小时和分钟?

时间:2018-08-02 11:49:31

标签: c# entity-framework linq model-view-controller

我的数据库中有DateTime列,例如2018-08-02 14:00:00。我想比较日期和时间(仅小时和分钟)。到目前为止,我已经使用

  

DbFunctions.CreateTime(x.BroadCastOn.Hour,x.BroadCastOn.Minute,0.00)

但是它没有给我所需的结果

var todaysDate= DateTime.Now;   
context.deliverys
          .Where(x => DbFunctions.TruncateTime(x.DeliveredOn)  == todaysDate.Date  && todaysDate.TimeOfDay == DbFunctions.CreateTime(x.DeliveredOn.Hour, x.DeliveredOn.Minute,0.00).ToList()  

2 个答案:

答案 0 :(得分:0)

看起来您的意图不是让秒为0,对吗? DateTime.Now几乎不会将秒数设置为0。也许您是想忽略秒数:

var todaysDate= DateTime.Now;   
context.deliverys
   .Where(x => 
       DbFunctions.TruncateTime(x.DeliveredOn)  == todaysDate.Date  &&
      todaysDate.ToString("HH:mm") ==   
       DbFunctions.CreateTime(x.DeliveredOn.Hour, x.DeliveredOn.Minute,0).ToString(@"hh\:mm")
   .ToList()

答案 1 :(得分:0)

简单

 context.deliverys
          .Where(x => DbFunctions.TruncateTime(x.DeliveredOn)  == todaysDate.Date  
&& todaysDate.TimeOfDay.Hours == x.DeliveredOn.Hour && todaysDate.TimeOfDay.Minutes== x.DeliveredOn.Minute).ToList()