Link to home
Start Free TrialLog in
Avatar of waqarali1999
waqarali1999

asked on

Time comparison using javascript

I need to compare  time to make sure that the first one is always greater than the other one. My time format is like "1:00 PM" .

e.g: if meeting start time is 1:00 PM
and meeting end time is 11:00 AM then it should give you an error that "meeting end time must be greater than meeting start time".

Thanks
Avatar of Bill-Hanson
Bill-Hanson
Flag of United States of America image

Try this:

start = Date("1:00 PM");
end = Date("11:00 AM");
difference_in_milliseconds = end - start
' If difference is negative, then the dates are out of order.
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

start = Date("1:00 PM");
end = Date("11:00 AM");

should be


start = new Date("1:00 PM");
end = new Date("11:00 AM");

but it does not work anyway (I thought it would too)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or use the script I wrote which does just that...
Yeah, but mine uses less CPU and has fewer lines of code!   :)
Perhaps or perhaps not. Who knows what the browser does with the string you use and the numbers I use...
Also mine is in a function, yadda yadda ;)
Here is a shorter one
<script>
function mil(str) {
  var t = str.split(':')
  return new Date(2007,0,1,  ((str.toLowerCase().indexOf('pm')!=-1)?(12+parseInt(t[0],10)):t[0]),parseInt(t[1],10),00).getTime();
}
var endTime = "12:59 AM";
var startTime = "1:00 PM";
if (mil(endTime)-mil(startTime)<0) alert('Please use an end time greater than start time')
</script>
Bottom line is that it really doesn't matter.  Either technique will work.  :)

Just FYI:

(1) When I say slower, I'm referring to the number of operations required.  All of the calls to split, toLowerCase, indexOf, parseInt, getTime, the array indexing, etc, takes time.  Nesting the operations causes even more stack push and pop operations.

(2) I think mine is easier to read.

(3) I could just as easily wrap my code into a function, but mine would only need one call.
1) true However I am sure the time spent doing what I do is negligible
2) true
3) true
(1) true
Avatar of LimMH
LimMH

mplungjan solution does not work if its 12:40AM and 01:12AM - it will throw error even though 12:40AM is before 1:12AM
Correct

Better version:
<script>

function mil(str) {
  var t = str.split(':')
  var hh = parseInt(t[0],10);
  var mm = parseInt(t[1],10);
  var isAM = str.toLowerCase().indexOf('am')!=-1;
  hh -= (isAM && hh==12)?12:0; 
  hh += (isAM)?0:12;
  var d = new Date(2007,0,1,hh,mm,00); // just a date not around daylightsaving
  return d.getTime();
}

var startTime = "11:00 AM";
var endTime = "1:00 PM";
if (mil(startTime)>mil(endTime)) alert('1. Please use an end time greater than start time')
startTime = "12:40 AM";
endTime = "1:12 AM";
if (mil(startTime)>mil(endTime)) alert('2. Please use an end time greater than start time')
startTime = "1:40 AM";
endTime = "12:12 AM";
if (mil(startTime)>mil(endTime)) alert('3. Please use an end time greater than start time')

</script>

Open in new window