?
Solved

How to compare two times in JQuery?

Posted on 2012-08-21
2
Medium Priority
?
426 Views
Last Modified: 2012-08-22
I have two asp:text box fields (TimeIn and TimeOut) that are being populated by times in this format:  "h:mm tt"

How can I compare these to prevent the entry of one time that occurs before another?

For example, I don't want a user to enter a TimeIn that is after the TimeOut.

Thanks.
0
Comment
Question by:jamesdean666
2 Comments
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 38316737
Here is an example
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
	$('#compare').click(function() {
		if (comparetime($('#input1').val(), $('#input2').val())) {
			alert('dates are wrong');
		}
	});
});
function comparetime(t1, t2)
{
	return Date.parse('01/01/2012 ' + t1) > Date.parse('01/01/2012 ' + t2);
}
</script>
</head>
<body>
Input 1 <input type="text" id="input1" /><br/>
Input 2 <input type="text" id="input2" />
<input type="button" id="compare" value="compare" />
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:jamesdean666
ID: 38320425
Thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question