Susen Maharjan
Susen Maharjan

Reputation: 537

How to accept date up to one months from today?

I have successfully implemented to reject the date time from past. Now I want to accept the date up to one months, i.e 30 days only from today. How can I achieve it in C#? Following code accepts the date of future only.

public class FutureDate : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            DateTime dateTime;
            var isValid = DateTime.TryParseExact(Convert.ToString(value),
                "d MMM yyyy",
                CultureInfo.CurrentCulture,
                DateTimeStyles.None,
                out dateTime
            );
            return (isValid && dateTime > DateTime.Now);
        }
    }

Upvotes: 2

Views: 78

Answers (1)

Susen Maharjan
Susen Maharjan

Reputation: 537

I've found the solution.

return (isValid && dateTime > DateTime.Now && dateTime <= DateTime.Now.AddDays(30));

Upvotes: 1

Related Questions