Possible Duplicate:
How to compare Dates in C#
This code of mine:
public static string getLogFileNameForDate(DateTime dt) { if (dt.Equals(DateTime.Now))
...fails even when the two dates are the same (date) because dt is assigned a value at startup (e.g. "6/18/2012 15:19:42"), and so the dates are not exactly the same, even though the year, month, and day are the same (value of DateTime.Now may be, say, "6/18/2012 15:30:13").
I know I can test it this way:
if ((dt.Year.Equals(DateTime.Now.Year) && (dt.Month.Equals(DateTime.Now.Month) && (dt.Day.Equals(DateTime.Now.Day))
...but that seems a bit Jethro*-like
What is the accepted/preferred method (no pun intended)?
To check if two dates are the same day, call the toDateString() method on both Date() objects and compare the results. If the output from calling the method is the same, the dates are the same day.
Try
if (dt.Date == DateTime.Now.Date)
It will only take the date portion and the timestamp will be 12:00:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With