Is there a RangeAttribute for DateTime?

后端 未结 3 1311
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 15:57

I have a Datetime field in my Model and need to validate it so that when it is created it has to fall between Now and 6 Years Prior. I have

3条回答
  •  伪装坚强ぢ
    2020-12-01 16:35

    Use this attribute:

    public class CustomDateAttribute : RangeAttribute
    {
      public CustomDateAttribute()
        : base(typeof(DateTime), 
                DateTime.Now.AddYears(-6).ToShortDateString(),
                DateTime.Now.ToShortDateString()) 
      { } 
    }
    

提交回复
热议问题