4

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

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • What are you doing there? Why do you convert a datetime from `DateTime.Now` to a string and then back to `DateTime`? Just use `DateTime.Today`. – Tim Schmelter Mar 16 '16 at 13:57
  • `DateTime _CurDate = DateTime.Today` would be simpler than parsing `Now` to strip off the time. You could also use `value.Date` to return just the date portion. – D Stanley Mar 16 '16 at 13:57
  • http://stackoverflow.com/a/683051/1693085 – John Bustos Mar 16 '16 at 13:58
  • this line is giving error `DateTime _dateJoin = DateTime.ParseExact(value.ToString(), "MM/dd/yyyy", null);` – Thomas Mar 16 '16 at 13:59
  • Im going to geus its a `FormatException`. Whats the value of `value` ? – Igor Mar 16 '16 at 14:00
  • object value has date like `Date = {03-16-2016 12:00:00 AM}` when execute this line `DateTime _dateJoin = DateTime.ParseExact(value.ToString(), "MM/dd/yyyy", null);` then getting error like `String was not recognized as a valid DateTime.` – Thomas Mar 16 '16 at 14:00
  • 1
    Is it a json (string) value or actually DateTime? The latter you can cast directly to DateTime. `DateTime _dateJoin = (DateTime)value;` Better yet is to just remove the parameter type `object` and make it `DateTime?` and check it for null before executing your greater than test. – Igor Mar 16 '16 at 14:02

2 Answers2

7

You just need to compare DateTime.Today and DateTime.Date:

if(_dateJoin.Date > DateTime.Today)
{
    // ...
}
else
{
    // ...
}

Update:

object value has date like Date = {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);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • yes i did that before but did not work. – Thomas Mar 16 '16 at 14:01
  • object value has date like Date = {03-16-2016 12:00:00 AM} when execute this line DateTime _dateJoin = DateTime.ParseExact(value.ToString(), "MM/dd/yyyy", null); then getting error like String was not recognized as a valid DateTime. – Thomas Mar 16 '16 at 14:01
  • i want to exclude time part from two date variable when comparing. how to achieve it. – Thomas Mar 16 '16 at 14:02
  • @Thomas: if your current culture doesn't use the AM/PM designator you can make it working with the overload. See my update. – Tim Schmelter Mar 16 '16 at 14:04
  • @Thomas: i've shown how to exclude the time portion, just use `DateTime.Today` and `DateTime.Date`. – Tim Schmelter Mar 16 '16 at 14:08
  • the code u gave here called `DateTime.Date` no such thing exist. – Thomas Mar 16 '16 at 14:24
  • how to exclude time portion from date? – Thomas Mar 16 '16 at 14:24
  • @Thomas: i dont call `DateTime.Date`. I'm calling `DateTime.Today` which is a static property. I'm using `_dateJoin.Date` which is an instance property, both in the `DateTime` struct. If you have a `DateTime` like `_dateJoin` you just have to use `Date` to get a `DateTime` which is the same day at midnight. `DateTime.Today` simply returns the `DateTime` of today's midnight. – Tim Schmelter Mar 16 '16 at 14:27
  • i enter in console and found DateTime.Today return date with time. here is fiddle https://dotnetfiddle.net/RvIG7d – Thomas Mar 16 '16 at 14:31
  • @Thomas: no, you are confusing the visual representation of a DateTime with it's pure value. A `DateTime` has no representation, it just has a value(like an integer). `DateTime.Today` returns a new DateTime where the time portion is "truncated". But if you output it you get also the hour and minutes which are zero. If you dont want that use methods like `DateTime.Today.ToShortDateString` or `DateTime.Today.ToString("d")`. https://dotnetfiddle.net/oIKnQQ – Tim Schmelter Mar 16 '16 at 14:34
5

Compare the date parts only:

int cmp = _dateJoin.Date.CompareTo(_CurDate.Date);
James Dev
  • 2,979
  • 1
  • 11
  • 16