[英]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.谢谢。
您可以使用DateTime.Now.Date
来获取没有时间部分的值,或者更准确地说,将时间部分设置为午夜。
正如您在问题中提到的, d
和DateTime.Now
都将采用 TimeStamp,如果您想排除 TimeStamp 意味着您必须使用d.Date
和DateTime.Today
(这将为您提供没有 TimeStamp 的今天的日期,并且等于DateTime.Now.Date
所以这一行listDatesNotAvailable.Where(d => d >= DateTime.Now)
应该是这样的:
listDatesNotAvailable.Where(d => d.Date >= DateTime.Today)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.