Good afternoon, the data in string format comes to me: '11: 43 'and '13: 14' Help to write the script correctly so that it calculates the difference between these two numbers, it should be 01:31. It is possible through Moment JS

    3 answers 3

    Perhaps you will be enough such a decision:

    let firstDate = '11:43'; let secondDate = '13:14'; let getDate = (string) => new Date(0, 0,0, string.split(':')[0], string.split(':')[1]); //получение даты из строки (подставляются часы и минуты let different = (getDate(secondDate) - getDate(firstDate)); let hours = Math.floor((different % 86400000) / 3600000); let minutes = Math.round(((different % 86400000) % 3600000) / 60000); let result = hours + ':' + minutes; console.log(result); 

    • Thank you, great solution! - Puvvl
    • one
      No need to make a date out of time. Time zones and any other nonsense can spoil the result. - Qwertiy
    • @Qwertiy And the time zone (and any other) is not assigned to both lines and the difference between the dates will not change. Or if I confuse something, I will be glad to hear from you a more specific example that will break this logic in order to correct my decision and at the same time get new knowledge. - Sergey Glazirin
    • @SergeyGlazirin, what if there is a daylight saving time between the resulting dates, for example? Or some range of missing dates? Or change of time zone? habrahabr.ru/post/146109 - Qwertiy
    • @Qwertiy Thank you for the rather comprehensive information. And now a little parsing. Daylight saving time is carried out at midnight, and this already turns out to go beyond one day and comparing hours and minutes is not enough. If there are only hours and minutes, then new Date () will substitute the current time zone, which will not affect the result. And if there are missing dates, then, most likely, you need more data to solve this problem, since again, it goes beyond one day, and the data format implies only hours and minutes. - Sergey Glazirin

    You can do this:

    1. Separate the first and second string values ​​separately for hours and minutes
    2. multiply the clock by 60 and add it to the minutes and you get one number in minutes
    3. it should be 11 * 60 + 43 = 703 and 13 * 60 + 14 = 794
    4. subtract the second minutes from the first minutes and take the answer modulo
    5. it will turn out | 703-794 | = 91
    6. then we simply divide the answer by 60 and get the number of hours, i.e. one
    7. and at the end we subtract from the answer the number of hours * 60, i.e. 91-60 * 1 = 31 and get the minutes

    Total 1 hour 31 minutes

    • Only subtract the first from the second and no module is needed. - Qwertiy
    • one
      And if the second value is less than the first? The module method is universal. - Stanley Wintergreen
    • No, the method with the module is controversial. First, the question says nothing about the fact that the second time may be less. Secondly, if some event began at 23:55 and ended at 00:05, then the correct duration is 00:10, and the module will give 23:50. More often, it will be necessary to add a day to a negative result than to process that the values ​​may be transferred in the wrong order. - Qwertiy
    • "if some event began at 23:55 and ended at 00:05," then it is important what date it started and what time it ended. The question says "calculated the difference between these two numbers." If we are talking about dates, then only Comrade Puvvl can judge us. - Stanley Wintergreen

    I added a little script if the time began at 23:43 and ended at 04:14 will give the correct answer.

     let firstDate = '23:43'; let secondDate = '04:14'; let getDate = (string) => new Date(0, 0,0, string.split(':')[0], string.split(':')[1]); let different = (getDate(secondDate) - getDate(firstDate)); let differentRes, hours, minuts; if(different > 0) { differentRes = different; hours = Math.floor((differentRes % 86400000) / 3600000); minuts = Math.round(((differentRes % 86400000) % 3600000) / 60000); } else { differentRes = Math.abs((getDate(firstDate) - getDate(secondDate))); hours = Math.floor(24 - (differentRes % 86400000) / 3600000); minuts = Math.round(60 - ((differentRes % 86400000) % 3600000) / 60000); } let result = hours + ':' + minuts; console.log(result)