Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7300
  • Last Modified:

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
0
waqarali1999
Asked:
waqarali1999
  • 6
  • 5
2 Solutions
 
Bill-HansonCommented:
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.
0
 
Michel PlungjanIT ExpertCommented:
<script>

function mil(str) {
  var t = str.split(':')
  var hh = parseInt(t[0],10);
  var mm = parseInt(t[1],10)
  hh += (str.toLowerCase().indexOf('pm')!=-1)?12:0;
  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('Please use an end time greater than start time')

</script>
0
 
Michel PlungjanIT ExpertCommented:

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)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Bill-HansonCommented:
mplungjan:
You're right about the missing "new".  My bad.

waqarali1999:
Since both times are on the same day, you can just prepend any date to the time values:

var start = "1:00 PM";
var end = "11:00 AM";
var dtStart = new Date("1/1/2007 " + start);
var dtEnd = new Date("1/1/2007 " + end);
difference_in_milliseconds = dtEnd - dtStart;
if (difference_in_milliseconds < 0)
{
      alert("End date is before start date!");
}
0
 
Michel PlungjanIT ExpertCommented:
Or use the script I wrote which does just that...
0
 
Bill-HansonCommented:
Yeah, but mine uses less CPU and has fewer lines of code!   :)
0
 
Michel PlungjanIT ExpertCommented:
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>
0
 
Bill-HansonCommented:
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.
0
 
Michel PlungjanIT ExpertCommented:
1) true However I am sure the time spent doing what I do is negligible
2) true
3) true
0
 
Bill-HansonCommented:
(1) true
0
 
LimMHCommented:
mplungjan solution does not work if its 12:40AM and 01:12AM - it will throw error even though 12:40AM is before 1:12AM
0
 
Michel PlungjanIT ExpertCommented:
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

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now