Reputation: 26418
I want to check the date is not less than 30 days from the current date. So i am having a condition as below:
date.CompareTo(new DateTime().AddDays(-30)) < 0
but doesn't working. cant we add a negative number to date? if not how to achieve this?
Upvotes: 1
Views: 282
Reputation: 985
You could do:
date < DateTime.Now.AddDays(-30);
This includes time as well. If you don't want that, try:
date < DateTime.Today.AddDays(-30);
Upvotes: 0
Reputation: 223207
Instead of new DateTime()
you need DateTime.Now
for current date
so your check should be:
date.CompareTo(DateTime.Now.AddDays(-30)) < 0
You can also do:
if(date < DateTime.Now.AddDays(-30))
EDIT: (from comment of @Rawling)
If you want to compare against the Date part of the DateTime, then you may use DateTime.Now.Date
or DateTime.Today
instead of DateTime.Now
which will point to current date with 12:00AM
time
date.Compare(DateTime.Now.Date.AddDays(-30)) < 0
Upvotes: 4