1

From the following html and javascript code i hope to compare the input time with current time! If the input time is less than 2 hours i want to print "Less time" in the label and if its more than 2 hours i want to print "sufficient time" in the label!

 
    function test(){
     var element = document.getElementById("time").value;
     var d = new Date();
        var n = d.getTime();
     if(element-n>2){
       document.getElementById("check").innerHTML="sufficient time";
     }
     else{
      document.getElementById("check").innerHTML="Less time";
     
     }
    }
 
    <html>
    
    <body>
     <form>
      <span>Time</span>
     <input type="time" id="time">
      
        <input type="button" value="CHECK"   onclick="test();"> <br>
      <label id="check"></label>
              <input class="button" type=reset name=reset value=Cancel>
     </form>
    </body>
    </html>

when i evaluate this i always get less time! How can i correct mycode?

Legends
  • 21,202
  • 16
  • 97
  • 123
  • 1
    The input value is a string of format `hh:mm:ss.ms`, so you cannot perform arithmetic operations directly. You'll have to parse it first. – Or B Jan 11 '17 at 17:37
  • i have no idea about the way to handle it :( – Sachini Karunaratne Jan 11 '17 at 17:38
  • [This SO thread](http://stackoverflow.com/q/18623783/2581562) will help you. You have to use moment.js, don't bother with an individual solution, it can get very tricky. – Legends Jan 11 '17 at 17:59

4 Answers4

4

Working example of your code

function test() {

  var element = document.getElementById("time").value;
  
  if (element == "") {
  alert("Please Enter Time");
    return false;  
  }
  else {
  
  // get system local time
  var d = new Date();
  var m = d.getMinutes();
  var h = d.getHours();
  if(h == '0') {h = 24}
  
  var currentTime = h+"."+m;
  console.log(currentTime);
 
  // get input time
  var time = element.split(":");
  var hour = time[0];
  if(hour == '00') {hour = 24}
  var min = time[1];
  
  var inputTime = hour+"."+min;
  console.log(inputTime);
  
  var totalTime = currentTime - inputTime;
  console.log(totalTime);
  
  if ((Math.abs(totalTime)) > 2) {
    document.getElementById("check").innerHTML = "sufficient time";
  } 
  else {
    document.getElementById("check").innerHTML = "Less time";

  }
    }
}
<html>

<body>
  <form>
    <span>Time</span>
    <input type="time" id="time" required>

    <input type="button" value="CHECK" onclick="return test();">
    <br>
    <label id="check"></label>
    <input class="button" type=reset name=reset value=Cancel>
  </form>
</body>

</html>
Object object
  • 872
  • 6
  • 18
1

You are not considering that input value in your field has to be parsed to a Date value to be of any use in calculation:

function test(){
    var timeNow = new Date();
    var tm = document.getElementById("time");
    var timeParts = tm.split(":");
    var inputTime = new Date(timeNow.getYear() , timeNow.getMonth() ,     timeNow.getDate() , parseInt(timeParts[0]), parseInt(timeParts[1]), 0, 0);

    var diff = Math.abs(timeNow.getTime() - inputTime.getTime());

    if( diff > 2*60*60*1000 ) 
        document.getElementById("check").innerHTML="sufficient  time";
    else
        document.getElementById("check").innerHTML="Less time";
     }

I am assuming you are checking for two hours before or after the current time. If not, feel free to remove the Math.abs and use accordingly.

dev8080
  • 3,950
  • 1
  • 12
  • 18
0

This is essentially a type mismatch. You are comparing a timestamp to a time string. The getTime() returns a timestamp that specifies a time on a particular date, whereas the time string is just a string such as 02:02 which doesn't specify a date.

You will need to collect date and time from inputs, then construct a Date object from the input, then compare two getTime() results. That way you will be comparing two timestamps.

It is important that the input includes the date because of what happens around midnight. If the current time is 23:30 and the input time is 01:15, how will your code know whether the user meant 1:15 tomorrow morning?

skeltoac
  • 167
  • 7
0

getTime() returns milliseconds, so to get two hours, simply multiply 1000 * 60 * 60 to get one hour in milliseconds.

element will contain a string, like "12:30" and must be converted, this one might help you further:

Convert string to time JavaScript (h:m)

Hope it helps.

Community
  • 1
  • 1
Tornseglare
  • 963
  • 1
  • 11
  • 24