Создайте форму, в которой пользователь может ввести дату/время начала и дату/время окончания события. Вот валидатор:
public class EventModelValidator : AbstractValidator<EventViewModel>
{
public EventModelValidator()
{
RuleFor(x => x.StartDate)
.NotEmpty().WithMessage("Date is required!")
.Must(BeAValidDate).WithMessage("Invalid date");
RuleFor(x => x.StartTime)
.NotEmpty().WithMessage("Start time is required!")
.Must(BeAValidTime).WithMessage("Invalid Start time");
RuleFor(x => x.EndTime)
.NotEmpty().WithMessage("End time is required!")
.Must(BeAValidTime).WithMessage("Invalid End time");
RuleFor(x => x.Title).NotEmpty().WithMessage("A title is required!");
}
private bool BeAValidDate(string value)
{
DateTime date;
return DateTime.TryParse(value, out date);
}
private bool BeAValidTime(string value)
{
DateTimeOffset offset;
return DateTimeOffset.TryParse(value, out offset);
}
}
Теперь я также хотел бы добавить проверку того, что EndDateTime > StartDateTime (объединенные свойства даты и времени), но не знаю, как это сделать.
Редактировать: Чтобы уточнить, мне нужно каким-то образом объединить EndDate + EndTime/StartDate + StartTime, т.е. DateTime.Parse(src.StartDate + " " + src.StartTime), а затем проверить EndDateTime и StartDateTime - как это сделать Я делаю это?