Search code examples
javascriptdateprompt

show remaining time left after user input


I am trying to show the remaining time left after the user inputs their answer.

so it's suppose to be like this. When does that course start? 2022-09-05 (user input) Today it is 32 days left until the course starts

I dont think its suppose to be that complicated but I cant make it work, I keep getting NaN or that it just isnt working.

I have checked MDN but I just dont get it.

The code looks like this.

    function start(timePassedIn) {
      return `Today it is ${timePassedIn} days left until the 
      course starts`;
    }

    const course = prompt("When does that course start? ");
    const starting = start(course);

  
    console.log(starting);

I removed all my attempts at the date so that you can give me fresh input.

Appreciate all the help I can get.


Solution

  • Can you try this.

    function start(timePassedIn) {
          return `Today it is ${timePassedIn} days left until the 
          course starts`;
     }
     function getDateDifference(inputDate) {
        const date1 = new Date(inputDate);
        const date2 = new Date();
        const diffTime = Math.abs(date1 - date2);
        const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
      return diffDays;
     }
     const course = prompt("When does that course start? ");
     const starting = start(getDateDifference(course));