1

I’m using System.Data.SqlTypes.SqlDateTime.MinValue and System.Data.SqlTypes.SqlDateTime.MaxValue to define the range for Datetime and Date field using following lines of code.

[Range(typeof(DateTime), "1/1/1753 12:00:00 AM", "12/31/9999 11:59:59 PM", ErrorMessage="\'D2\' must be within 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.")]
public System.Nullable<System.DateTime> D2{get;set;}//;

But when I entered date 11/08/2016 12:00 am into textbox it throws an error message

'D2' must be within 1/1/1753 12:00 AM and 12/31/9999 11:59 PM.

So please let me know what wrong in above code.

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Pankaj
  • 3,131
  • 4
  • 14
  • 22
  • http://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format. you need to setup the datetime variable different, not like the display value – Kapein Nov 11 '16 at 08:01
  • did you try to use ISO-Format notation (i.e. YYYY-MM-DD HH:mm:SS). Your format depends on the locale and might create "interesting" behaviour therefore. – Stephen Reindl Nov 11 '16 at 08:10

1 Answers1

0

you can use IValidatableObject interface for your class and check date validation there like below

public class Sample : IValidatableObject
{
    public DateTime Date { set; get; }
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Date > new DateTime(2012, 12, 12))
        {
            yield return new ValidationResult("you cant enter it");
        }
    }
}
Salar Afshar
  • 229
  • 1
  • 2
  • 13