当前位置: 动力学知识库 > 问答 > 编程问答 >

How to retrieve records based on Yesterday date in c#?

问题描述:

Date_Of_Event.Date <= DateTime.Today.Date.AddDays(-1))

网友答案:

You have the comparison backwards

// This should work in LINQ-to-SQL / EntityFramework
Date_Of_Event >= DateTime.Today.AddDays(-1)
&&
Date_Of_Event < DateTime.Today

or

// This will work in LINQ-to-Objects or anywhere else, really.
Date_Of_Event.Date == DateTime.Today.AddDays(-1)

As a side note, it may be a good idea to store the value of DateTime.Today in a variable, so that weird bugs don't show up around midnight (or 11:00pm or 1:00am depending on daylight saving time).

网友答案:
Date_Of_Event.Date == DateTime.Today.AddDays(-1)

This will remove the time part of the date time, and keep only the date so you can check for equality instead of the time being inside the correct interval.

分享给朋友:
您可能感兴趣的文章:
随机阅读: