tags:

views:

732

answers:

10

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

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
}
+3  A: 

Try

if (newsStory.Date == DateTime.Now.Date) 
{ /* Story happened today */ }
else
{ /* Story didn't happen today */ }
Stephen Newman
@Lucero - thanks for sorting the formatting out for me :)
Stephen Newman
+7  A: 
if( newsStory.Date == DateTime.Today )
{
    // happened today
}
Dave
+10  A: 
if (newsStory.Date == DateTime.Today)
{

}
else
{

}

Should do the trick.

Cloud
...if newsStory.Date is actually only the date portion, without time. ;)
Lucero
...which it is, if newsStory is a DateTime
stevemegson
@stevemegson +1 :)
Philip Wallace
Well, from what I can see it may just also be some custom class which happens to expose a `Day`, `Month` and `Year` property, no?
Lucero
well, DateTime is a part of the .NET Framework Class Library, so while it's *possible* that the OP created his own class that happens to share the same name, you'd have to wonder why he would ask StackOverflow how to use a custom class he created and expect us to magically know how it worked. ;)
Brian Schroth
@Lucero occam's razor
Dave
@Dave I really didn't see newsStory as DateTime when I answered the question - maybe due to its naming I thought of it as being a custom class.
Lucero
@Lucero that's possible however the question title reads 'two DateTimes'. ;) I guess that's where we got that missing type from.
Cloud
+1  A: 

As Guillame suggested, compare values of Date properties.

Anton Gogolev
+4  A: 

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

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).

Lucero
+1 for a more complete answer
JustLoren
A: 

you could use DateTime.Now.DayOfYear

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

 }
 else
 { // story didn't happen today

 }
Eclipsed4utoo
I don't think this will work. If today is 10/21/2009, and newsStory is 10/21/2008 - it will return true (I didn't take leap years into account here).
Philip Wallace
This won't discriminate between yearsnew DateTime(2009, 10, 01).DayOfYear == new DateTime(1900, 10, 01).DayOfYear;
Dave
You are correct. I totally missed that.
Eclipsed4utoo
+1  A: 

Try this:

newsStory.Date == DateTime.Today
Philip Wallace
A: 

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.

ShellShock
+1  A: 

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.

Brian Schroth
I think a slowdown(if if exists) will be so small it doesn't matter in almost all cases. Certainly a case of premature optimization.
Esben Skov Pedersen
Agreed - these days I find it's increasingly difficult to shake out premature optimization as the modern coding world is more about readability and maintainability. Old habits die hard!
Peter Bridger
+1  A: 

FYI,

newsStory.Date == DateTime.Today

will return the same compare result as coding

newsStory == DateTime.Today

where newsStory is a DateTime object

.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.

guck
Interesting point, thank you
Peter Bridger