C# 如何检查DateTime今天是否发生?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1601609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:16:23  来源:igfitidea点击:

How to check if a DateTime occurs today?

c#.netdatetime

提问by Peter Bridger

Is there a better .net way to check if a DateTime has occured 'today' then the code below?

有没有更好的 .net 方法来检查 DateTime 是否发生在“今天”然后下面的代码?

if ( newsStory.WhenAdded.Day == DateTime.Now.Day &&
     newsStory.WhenAdded.Month == DateTime.Now.Month &&
     newsStory.WhenAdded.Year == DateTime.Now.Year )
{ 
    // Story happened today
}
else
{ 
    // Story didn't happen today
}

采纳答案by pyrocumulus

if (newsStory.WhenAdded.Date == DateTime.Today)
{

}
else
{

}

Should do the trick.

应该做的伎俩。

回答by Stephen Newman

Try

尝试

if (newsStory.Date == DateTime.Now.Date) 
{ /* Story happened today */ }
else
{ /* Story didn't happen today */ }

回答by Dave Downs

if( newsStory.Date == DateTime.Today )
{
    // happened today
}

回答by Anton Gogolev

As Guillamesuggested in a comment, compare values of Dateproperties:

正如Guillame在评论中建议,比较Date属性的值:

newStory.Date == DateTime.Now.Date

回答by Lucero

If NewsStory was using a DateTime also, just compare the Date property, and you're done.

如果 NewsStory 也使用 DateTime,只需比较 Date 属性,就完成了。

However, this depends what "today" actually means. If something is posted shortly before midnight, it will be "old" after a short time. So maybe it would be best to keep the exact story date (including time, preferably UTC) and check if less than 24 hours (or whatever) have passed, which is simple (dates can be subtracted, which gives you a TimeSpan with a TotalHours or TotalDays property).

然而,这取决于“今天”的实际含义。如果在午夜前不久发布了一些东西,它会在很短的时间后“旧”。因此,也许最好保留确切的故事日期(包括时间,最好是 UTC)并检查是否已经过去了不到 24 小时(或其他任何时间),这很简单(可以减去日期,从而为您提供带有 TotalHours 的 TimeSpan或 TotalDays 属性)。

回答by Ryan Alford

you could use DateTime.Now.DayOfYear

你可以用 DateTime.Now.DayOfYear

 if (newsStory.DayOfYear == DateTime.Now.DayOfYear)
 { // story happened today

 }
 else
 { // story didn't happen today

 }

回答by Philip Wallace

Try this:

尝试这个:

newsStory.Date == DateTime.Today

回答by Polyfun

How about

怎么样

if (newsStory.DayOfYear == DateTime.Now.DayOfYear)
{ // Story happened today
}

But this will also return true for 1st January 2008 and 1st January 2009, which may or may not be what you want.

但这也将在 2008 年 1 月 1 日和 2009 年 1 月 1 日返回 true,这可能是您想要的,也可能不是。

回答by Brian Schroth

well, DateTime has a "Date" property and you could just compare based on that. But looking at the docs it seems that getting that property actually instantiates a new datetime with the time component set to midnight, so it may very well be slower than accessing each individual component, although much cleaner and more readable.

好吧,DateTime 有一个“Date”属性,您可以根据它进行比较。但是查看文档似乎获取该属性实际上实例化了一个新的日期时间,并将时间组件设置为午夜,因此它很可能比访问每个单独的组件要慢,尽管更清晰且更具可读性。

回答by guck

FYI,

供参考,

newsStory.Date == DateTime.Today

newsStory.Date == DateTime.Today

will return the same compare result as coding

将返回与编码相同的比较结果

newsStory == DateTime.Today

newsStory == DateTime.Today

where newsStoryis a DateTimeobject

其中newsStory是一个DateTime对象

.NET is smart enough to determine you want to compare based on Date only and uses that for the internal Compare. Not sure why, and actually having trouble finding documentation for this behaviour.

.NET 足够聪明,可以确定您只想根据日期进行比较,并将其用于内部比较。不知道为什么,实际上很难找到这种行为的文档。