Reputation: 9323
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
}
Upvotes: 131
Views: 100321
Reputation: 115897
As Guillame suggested in a comment, compare values of Date
properties:
newStory.Date == DateTime.Now.Date
Upvotes: 7
Reputation: 71
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.
Upvotes: 7
Reputation: 9300
if (newsStory.WhenAdded.Date == DateTime.Today)
{
}
else
{
}
Should do the trick.
Upvotes: 260
Reputation: 552
You can implement a DateTime extension method.
Create new class for your extension methods:
namespace ExtensionMethods
{
public static class ExtensionMethods
{
public static bool IsSameDay( this DateTime datetime1, DateTime datetime2 )
{
return datetime1.Year == datetime2.Year
&& datetime1.Month == datetime2.Month
&& datetime1.Day == datetime2.Day;
}
}
}
And now, everywhere on your code, where do you want to perform this test, you should include the using:
using ExtensionMethods;
And then, use the extension method:
newsStory.WhenAdded.IsSameDay(DateTime.Now);
Upvotes: 8
Reputation: 105
My solution:
private bool IsTheSameDay(DateTime date1, DateTime date2)
{
return (date1.Year == date2.Year && date1.DayOfYear == date2.DayOfYear);
}
Upvotes: 14
Reputation: 2457
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.
Upvotes: 1
Reputation: 1977
Try
if (newsStory.Date == DateTime.Now.Date)
{ /* Story happened today */ }
else
{ /* Story didn't happen today */ }
Upvotes: 19
Reputation: 9651
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.
Upvotes: -2
Reputation: 7594
you could use DateTime.Now.DayOfYear
if (newsStory.DayOfYear == DateTime.Now.DayOfYear)
{ // story happened today
}
else
{ // story didn't happen today
}
Upvotes: -6
Reputation: 60276
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).
Upvotes: 8