1

Hi I am a beginner I wanted to compare my two variables to each other using javascript if ts is greater than timecomp display alert "Okay" else if less than ts display alert "error"

like example ts is 6:15 and time comp is 6:00 alert okay message will display but if less than will display alert message error

<script> 

$(document).ready( function(){
var timecomp = "6:00"

var a = $('select[name="hours"] option:selected').val();
var a1 = $('select[name="mins"] option:selected').val();
var ts = a +":" a1;


 if( ts> timecomp)
{

alert("Okay");

}
else if ( ts<timecomp){


alert("Error");
}
});

</script>
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
ROD BAL
  • 31
  • 5
  • Provide ur html please – Maheer Ali Jan 30 '19 at 08:11
  • Possible duplicate https://stackoverflow.com/questions/6212305/how-can-i-compare-two-time-strings-in-the-format-hhmmss – techie_28 Jan 30 '19 at 08:13
  • 1
    You can't exactly compare the variables. The output would *sometimes* match but it's not guaranteed to, since you are doing string comparison. In that case `06:15` is more than `06:00` and even `05:15` and still less than `16:15`. But `6:15` - without the leading zero is considered more than `16:15`. – VLAZ Jan 30 '19 at 08:14
  • 1
    Possible duplicate of [How can I compare two time strings in the format HH:MM:SS?](https://stackoverflow.com/questions/6212305/how-can-i-compare-two-time-strings-in-the-format-hhmmss) – Nick Jan 30 '19 at 08:29

5 Answers5

3

If your strings are in the format of "HH:MM:SS" and your times are in 24 hour time you can compare the two strings using inequalities like you wish:

$(document).ready(function() {
  var timecomp = "06:00:00" // add :00 to the end for correct format 

  var a = "05"; // change these to see it working
  var a1 = "30"; // change these to see it working
  var ts = a + ":" + a1 + ":00"; // add ":00 for the seconds format"

  if (ts > timecomp) {
    alert("Okay");
  } else if (ts < timecomp) {
    alert("Error");
  } else {
    alert("Equal");
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
3
var a = "10:20:45";
var b = "5:10:10";

var timeA = new Date();
timeA.setHours(a.split(":")[0],a.split(":")[1],a.split(":")[2]);
timeB = new Date();
timeB.setHours(b.split(":")[0],b.split(":")[1],b.split(":")[2]);


if(timeA>timeB) 
{
alert("A is large");
}
else{
alert("B is large");
}

enter link description here

Jacky
  • 771
  • 3
  • 20
1

Define the times with setHours() and use new Date() in the conditions.

Run code Snippet

var timecomp = new Date().setHours(6, 0, 0),
  a = 6, //$('select[name="hours"] option:selected').val();
  a1 = 15, // $('select[name="mins"] option:selected').val();
  ts = new Date().setHours(a, a1, 0);


if (new Date(ts).toLocaleTimeString() > new Date(timecomp).toLocaleTimeString()) {

  alert("Okay");

} else if (new Date(ts).toLocaleTimeString() < new Date(timecomp).toLocaleTimeString()) {

  alert("Error");
}
O.O
  • 1,389
  • 14
  • 29
0

You can compare Date objects.

var date1 = new Date(),
    date2 = new Date();

date1.setThours($('select[name="hours"] option:selected').val())
date1.setMinutes($('select[name="mins"] option:selected').val())

console.log(date1 > date2)
Vitalii
  • 161
  • 1
  • 10
0

I would suggest using setHours() function instead of comparing strings.

So you will have variables:

var startHour = new Date();
startHour.setHours(18, 00, 0); // 6:00 pm - 18:00
var endHour = new Date();
endHour.setHours(a, a1, 0); // 6.15 pm - 18:15

And the alerts:

if(endHour >= startHour){
    console.log("Okay");
}else{
    console.log("Error");
}
P. Bitos
  • 1
  • 1
  • 3