-1

I have a task where the user can input the time (hour, minute, second) then he can give a second time (hour, minute, second) but this second time has to be in the same day as the first one and it have to be a later one. Example the user inputs 12:59:59, then 13:00:00. If its correct then it have to substract them.

I have been trying if the second hour is bigger then substract the hour, but the problem is with the minutes / seconds. I can't substract 0 from 59 or so..

    /* Első időpont
    var a = prompt("Add meg hogy hány óra van.");
    var b = prompt("Add meg hogy hány perc van.");
    var c = prompt("Add meg hogy hány másodperc van.");
// Második időpont
    var d = prompt("Adj meg egy későbbi órát.");
    var e = prompt("Add meg hogy hány perc van.");
    var f = prompt("Add meg hogy hány másodperc van.");

    if(isFinite(a) && isFinite(b) && isFinite(c)) {
        if(a > d) {
            document.write();
        }
    } else {
        document.write("Nem számot adtál meg.");
    }*/
Kalip
  • 151
  • 1
  • 11

1 Answers1

0

Taking 01/01/2000 as date, you can do this

if(Date.parse('Sat, 01 Jan 2000 13:00:00') - Date.parse('Sat, 01 Jan 2000 12:59:59')>0)
{
   //do whatever you want
}
lone_worrior
  • 232
  • 2
  • 15
  • And if `Date.parse('01/01/2000 13:00:00')` returns an invalid Date? – RobG Oct 30 '17 at 11:59
  • why would `Date.parse('01/01/2000 13:00:00')` return invalid date? – lone_worrior Oct 30 '17 at 12:01
  • Because it is not a format that is required to be parsed by [*ECMA-262*](http://ecma-international.org/ecma-262/8.0/#sec-date-time-string-format). – RobG Oct 30 '17 at 12:02
  • The only format supported by [*ECMA-262*](http://ecma-international.org/ecma-262/8.0/#sec-date-time-string-format) is a subset of the ISO 8601 extended format. Support for any other format is implementation dependent, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Oct 30 '17 at 23:14