1

I would like to come to a code that show is a certain date is still valid and for how long.

The code I've come up with so far can check if the date is not in the future. But I'm unable to how if the date is still valid or not. Can someone help me?

function TheorieFunction() {
  var startDate = new Date(document.getElementById('DateTheorie').value);
  var today = new Date();

  if (startDate.getTime() > today.getTime()) {
    alert("Error: Given date is in the future");
  }
  if (startDate.getTime() > today.get(Calendar.YEAR) - 3) {
    document.getElementById("Theorietxt").innerHTML = "Your theorie is still valid for" + today.getTime() - startDate.getTime()
    "days";
  } else {
    document.getElementById("Theorietxt").innerHTML = "Your theorie is invalid";
  }
}
Date theorie exam: <input type="date" id="DateTheorie" value="2017-01-01">
<span class="validity"></span>
<button id=Btntheorie onclick="TheorieFunction()">Check</button>

<p id="Theorietxt"></p>
Mara Black
  • 1,666
  • 18
  • 23
  • What do you mean by "date is still valid"? Try to follow [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Andreas Jan 21 '20 at 09:19
  • The inputed date can not be older than 3 years from today If longer ago than 3 years the user should be told his exam is no longer valid – wendy schollaert Jan 21 '20 at 09:49

1 Answers1

2

Are you looking for a solution like this? Making use of date difference function from this post

Edit: i have used only basic year difference calculation like 356 days per year. If you want the exact year difference you need to go for moment.js difference function

You can find the implementation using moment js in this fiddle

function TheorieFunction() {
    var startDate = new Date(document.getElementById('DateTheorie').value);
    var today = new Date();

    if (startDate.getTime() > today.getTime()) {
        alert("Error: Given date is in the future");
        return;
    }            
    if(startDate.valueOf() !== NaN) {
        const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
        const diffDays = Math.round(Math.abs((today - startDate) / oneDay));
        if (diffDays < 3 * 365) {
            document.getElementById("Theorietxt").innerHTML="Your theorie is still valid for" + (diffDays) +  "days";
        }
        else {
            document.getElementById("Theorietxt").innerHTML="Your theorie is invalid";
        }
    } else {
        alert('In Valid Date selected');
    }
}
Date theorie exam: <input type="date" id="DateTheorie" value="2020-01-01" />
<span class="validity"></span>
<button id="Btntheorie" onclick="TheorieFunction()">Check</button>
<p id="Theorietxt"></p>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • Yes, this is sort of what I'm looking for. But the " else if (startDate.getTime() < today.getTime())" should be different. If the date is less than 3 years from today: "Theorie is still valid until (date+3years) [instead of valid for ... days] Else: "Your theorie is invalid" Any solution to that? Thanks in advance !! – wendy schollaert Jan 21 '20 at 10:09
  • @wendyschollaert I have updated the solution please check. – Nitheesh Jan 21 '20 at 10:23
  • You are my hero !! – wendy schollaert Jan 21 '20 at 11:54
  • @wendyschollaert if this is the solution that you are looking for, please approve my answer. :) – Nitheesh Jan 21 '20 at 13:09