-3

This is the code I have to use for a little school homework. Now when I type a date in the prompt it's a string and it needs to be converted to a number to work. I'm still a beginner in JavaScript so I don't really know what to do.

<script>
    enddate = prompt('What is the end date JJJJ-MM-DD?'); 
    var deadline = enddate; 
    console.log(deadline); 
    var now = Date.now(); 
    var day = 86400000; 
    var remaining = ; //help me here
    document.write('<br>There are: ' + remaining.toFixed(0) + ' days left!');
</script>
509216
  • 1
  • 1
    Possible duplicate of [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Clyde Lobo Nov 27 '18 at 10:19

1 Answers1

0

var input = prompt('What is the end date JJJJ-MM-DD?'); 
var endDate = new Date(input);
var nowDate = new Date();
var difference = Math.abs(nowDate.getTime() - endDate.getTime());
var remaining = Math.ceil(difference / (1000 * 60 * 60 * 24));
document.write('<br>There are: ' + remaining + ' days left!');

I think you are looking for the getTime method of the Date class. getTime returns the milliseconds since Jan 1, 1970. After having both values you can get the difference and turn them into days ( 1000 milliseconds = 1 second, 60 seconds = 1 minute, 60 minutes = 1 hour, 24 hours = 1 day).

Matthi
  • 1,073
  • 8
  • 19
  • `I pointed the contributor to the right topic` - where? All I see are function names and descriptions? I'm not unhappy, just trying to help you make a better answer.. will avoid helping in future :) – treyBake Nov 27 '18 at 12:17