In JS, there is a variable var a = '14:35' , the value of a changes dynamically, it gets time. It is necessary to compare with the browser time, the browser time is var b = '14:37' . If the browser time is more than 2 minutes, that is, say, 14:37, then output the alert('опоздал на 2 минуты') . How to implement this?
- oneIf relevant, then also pay attention to: the transition time after midnight, month, year; time zones and summer-winter time; the ability of the user to transfer the local time of the computer to ANY arbitrary. - Kromster
- Of course, it's actual! Yes, we need to deal with this somehow. Is there any average clock format? Could you refer to something so that it does not depend on the user's format? - elik
- You can not trust the user, from the word at all . Compare the time on the server. In most cases, however, paranoid checks are not worth the time spent on them. Your task is to find a middle ground between labor costs and the "gain" from the additional correctness of the algorithm. - Kromster
- @Kromster yeah) okay, thanks go think where it’s like the middle of what to base on - elik
- @Kromster thanks again very much - elik
|
1 answer
var a = '23:59'; var b = '00:10'; //разделяем минуты и часы, часы умножаем на 60. //Минуты умножаем на 1, чтобы преобразовать в число var amin=a.split(':')[0]*60+a.split(':')[1]*1; var bmin=b.split(':')[0]*60+b.split(':')[1]*1; //Если второе время меньше первого больше чем на 12 часов, добавим 24 часа: if(bmin+60*12<amin){ bmin+=60*24 } console.log('первое время в минутах', amin) console.log('второе время в минутах', bmin) var c=bmin-amin if(c > 0) alert('опоздал на '+c+' минут'); - Will your example work correctly when changing dates? For example,
23:59and00:10- Kromster - No, but you can try to fix it - Crantisz
- oneWhen changing dates, you will still need to specify dates, which is somewhat beyond the boundaries of the described task. - Sasha Omelchenko
- You can do this, for example, (corrected in response) - Crantisz
- @Kromster thanks Comrade. You helped me in advance. Thank you so much. And then you would fight again with 12 hours. - elik
|