i tried this way but getting error. here is my code.
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
DateTime _dateJoin = DateTime.ParseExact(value.ToString(), "MM/dd/yyyy", null);
DateTime _CurDate = DateTime.ParseExact(DateTime.Now.ToString(), "MM/dd/yyyy", null);
int cmp = _dateJoin.CompareTo(_CurDate);
if (cmp > 0)
{
return ValidationResult.Success;
}
else if (cmp < 0)
{
return new ValidationResult(ErrorMessage);
}
else
{
return ValidationResult.Success;
}
}
the value variable has valid date with time portion too. thanks
You just need to compare DateTime.Today
and DateTime.Date
:
if(_dateJoin.Date > DateTime.Today)
{
// ...
}
else
{
// ...
}
Update:
object
value has date likeDate = {03-16-2016 12:00:00 AM}
when execute this line
DateTime _dateJoin = DateTime.ParseExact(value.ToString(), "MM/dd/yyyy", null);
then i'm getting error like String was not recognized as a valid DateTime. –
That's a different issue, you have to use the correct format provider:
DateTime _dateJoin = DateTime.Parse(value.ToString(), CultureInfo.InvariantCulture);
with ParseExact
(not necessary in this case):
DateTime _dateJoin = DateTime.ParseExact(value.ToString(), "MM-dd-yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
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