1

Trying to find a way to validate if an input date chosen by the user, is older than the current date - 10 days. However, I'm stuck on this bit of code.

var date = document.getElementById("deliveryDate").value;
var varDate = new Date(date); 
var today = new Date();

if(varDate < today.setDate(today.getDate() - 10)) {
    alert("works!");
}
3
  • 1
    Dates are hard in JS. I suggest the use of date-fns.org
    – Dominik
    Oct 18, 2020 at 20:49
  • Is the 10 days, 24hrs * 10 from current time, or start of day..?
    – Keith
    Oct 18, 2020 at 20:50
  • Date inputs return the date in YYYY-MM-DD format, which is parsed as UTC with the time set to 00:00:00 UTC also. new Date() includes the current time. I don't see where you've taken those factors into account.
    – RobG
    Oct 19, 2020 at 2:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.