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

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
• 6
• 5
2 Solutions

Commented:
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

IT 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";

</script>
0

IT 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

Commented:
mplungjan:

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

IT ExpertCommented:
Or use the script I wrote which does just that...
0

Commented:
Yeah, but mine uses less CPU and has fewer lines of code!   :)
0

IT ExpertCommented:
Perhaps or perhaps not. Who knows what the browser does with the string you use and the numbers I use...
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";
</script>
0

Commented:
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

IT ExpertCommented:
1) true However I am sure the time spent doing what I do is negligible
2) true
3) true
0

Commented:
(1) true
0

Commented:
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

IT 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>
``````
0

Featured Post

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