There is such a task: there is a certain date, for example, 02/25/2017 20:32:57 and you need to determine whether 30 minutes have passed since the date was fixed, i.e. if now we have 21:00 then 30 minutes have passed, but if less, then we need to get the number of minutes and seconds that remained before 30 minutes have passed.

I managed to get the difference between the current date / time and the one given with the help of the library of momentjs:

var date = moment('2017-02-25T20:32:57.17282'); var timeDiff = moment.utc(moment().diff(date)).format("HH:mm:ss"); console.log(timeDiff); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script> 

But now I do not understand how from 30 minutes I deduct the obtained value and get a string that contains the remaining number of minutes and seconds.

If someone faced a similar task or just knows how to solve, I will be grateful for the help. Thank.

    1 answer 1

    It is assumed that the time is given according to local time.

     var diff = moment() - moment('2017-02-25T22:32:57.17282'); // Получится, в миллисекундах, сколько прошло времени. var rest = 30 * 60 * 1000 - diff; // Это сколько осталось, в миллисекундах, до истечения 30 минут. var minutes = Math.round(rest / 1000 / 60); var seconds = Math.round(rest / 1000 - minutes * 60); // Это сколько осталось, в минутах и секундах, соответственно.