How to check if a DateTime occurs today?
C#.NetDatetimeC# Problem Overview
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
}
C# Solutions
Solution 1 - C#
if (newsStory.WhenAdded.Date == DateTime.Today)
{
}
else
{
}
Should do the trick.
Solution 2 - C#
if( newsStory.Date == DateTime.Today )
{
// happened today
}
Solution 3 - C#
Try
if (newsStory.Date == DateTime.Now.Date)
{ /* Story happened today */ }
else
{ /* Story didn't happen today */ }
Solution 4 - C#
My solution:
private bool IsTheSameDay(DateTime date1, DateTime date2)
{
return (date1.Year == date2.Year && date1.DayOfYear == date2.DayOfYear);
}
Solution 5 - C#
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).
Solution 6 - C#
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);
Solution 7 - C#
As Guillame suggested in a comment, compare values of Date
properties:
newStory.Date == DateTime.Now.Date
Solution 8 - C#
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.
Solution 9 - C#
Try this:
newsStory.Date == DateTime.Today
Solution 10 - C#
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.
Solution 11 - C#
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.
Solution 12 - C#
you could use DateTime.Now.DayOfYear
if (newsStory.DayOfYear == DateTime.Now.DayOfYear)
{ // story happened today
}
else
{ // story didn't happen today
}