Date and Time Validations
Comparing dates in validation logic
Overview
Comparing Dates
// Ensure trip start is today or later and trip end is after start
// get today's date
const today = new Date(new Date().toISOString().slice(0,10));
// make sure and trip-start-date is >= today
if (TripStartDate.value && TripEndDate.value) {
const tripStartDate = new Date(TripStartDate.value);
const tripEndDate = new Date(TripEndDate.value);
if (tripStartDate.getTime() < today.getTime()) {
// The date is earlier than today, so set a validation error on the trip-start-date field
TripStartDate.setError('Start date must be on or after today');
}
if (tripEndDate.getTime() <= tripStartDate.getTime()) {
// The end date is not after start, so set a validation error on the trip-end-date field
TripEndDate.setError('End date must be on or after Start Date');
}
}
Comparing Dates with a Time part
Last updated
Was this helpful?