C# attribute to check whether one date is earlier than the other

前端 未结 6 1687
孤独总比滥情好
孤独总比滥情好 2021-02-07 15:05

I have a ViewModel for my MVC4 Prject containing two DateTime properties:

[Required]
[DataType(DataType.Date)]
public DateTime RentDate { get; set; }

[Required]         


        
6条回答
  •  你的背包
    2021-02-07 15:37

    If you're using .Net Framework 3.0 or higher you could do it as a class extension...

        /// 
        /// Determines if a DateTime falls before another DateTime (inclusive)
        /// 
        /// The DateTime being tested
        /// The DateTime used for the comparison
        /// bool
        public static bool isBefore(this DateTime dt, DateTime compare)
        {
            return dt.Ticks <= compare.Ticks;
        }
    
        /// 
        /// Determines if a DateTime falls after another DateTime (inclusive)
        /// 
        /// The DateTime being tested
        /// The DateTime used for the comparison
        /// bool
        public static bool isAfter(this DateTime dt, DateTime compare)
        {
            return dt.Ticks >= compare.Ticks;
        }
    

提交回复
热议问题