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

c# - Including dates from today onwards using DateTime.Now

问题描述:

I have this code:

public void SetPublisherDatesNotAvailable(string strPublisher, DateTime[]listDatesNotAvailable)

{

if (_PublisherData.PublisherDictionary.ContainsKey(strPublisher))

{

Publisher pPublisher = _PublisherData.PublisherDictionary[strPublisher];

pPublisher.Availability.DatesNotAvailable =

new List<DateTime>(listDatesNotAvailable.Where(d => d >= DateTime.Now)); // Ignore past dates

_PublisherData.PublisherDictionary[strPublisher] = pPublisher;

}

}

But it is failing to include any entry with todays date. It seems to be taking into account the TimeStamp too? That would explain it. But I only want to exclude it if it was yesterdays date.

How do I fix this?

Thanks.

网友答案:

As you mentioned in the question, Both d and DateTime.Now will take TimeStamp as well, if you wanted to exclude the TimeStamp means you have to use d.Date and also DateTime.Today(which will give you today's date without TimeStamp, and are equal to DateTime.Now.Date So This line listDatesNotAvailable.Where(d => d >= DateTime.Now) should be like this:

listDatesNotAvailable.Where(d => d.Date >= DateTime.Today)
网友答案:

You can use DateTime.Now.Date to get the value without the time component, or more precisely, with the time component set to midnight.

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