Fullbalanced Fullbalanced - 7 days ago 6
C# Question

How to compare date in format "dd/mm" with DateTime.Now

Please advise how I can check that current date (

DateTime.Now
) is in diapason of dates in format "
dd/mm
". For example -
01.01 <= DateTime.Now <= 01.03
- Current date more that
1st of January
but less that
1st of March

Answer

Let dateStrFrom be the first input ie, the From Date and dateStrTo be the second input ie, the To date. Then you can use DateTime.TryParseExact to convert it into the required DateTime Object for processing your comparison.

I hope that you are looking for something like this :

string dateStrFrom = "01.01"; 
string dateStrTo = "01.03";
DateTime dateFrom, dateTo;
DateTime.TryParseExact(dateStrFrom, "dd.MM", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateFrom);
DateTime.TryParseExact(dateStrTo, "dd.MM", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTo);
if (dateFrom <= DateTime.Now && dateTo <= DateTime.Now)
{ 
    // code here this will be the true condition for you
}
Comments