Fullbalanced Fullbalanced - 1 year ago 70
C# Question

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

Please advise how I can check that current date (

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download