I have a date field in my project which should allow only future dates from the saved date. For example, if 11/01/2018 is the initial date, when we are editing it should only allow me to select dates after 11/01/2018.
Can someone please help me with this? Thanks in advance!
// Save a date. You can pass any date as first parameter to the constructor,
// or just use today's date if you don't pass anything, like below
const savedDate = new Date();
// Create function which checks if date parameter is bigger than saved date
const isFuture = date => new Date(date.toDateString()) > savedDate;
// Get the date from an input field
const dateInput = new Date(document.querySelector('#dateField').value);
if (!isFuture(dateInput)) {
// Date input is before or on the saved date
alert('Date must be in the future!');
}
else {
// work with future date...
}
Gijo Varghese
A WordPress speed enthusiast
Marco Alka solution looks good. Also, check out momentjs if you need to deal a lot with dates.