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


how to compare times in javascript...?

Posted on 2003-03-12
Medium Priority
Last Modified: 2008-02-01

Can anyone tell me how to program the following problem?

1st time: 1:00  PM
2nd time: 11:00 AM

The 2nd time is not supposed to be less than 1:00PM. How to compare the times and validate to ensure that the 1st time is before the 2nd time?

Hoping to hear from u soon....


Question by:StrawBerry
LVL 14

Expert Comment

ID: 8119022
parse out the : and compare the num :

function compareTime(time1,time2)
var iTime1=parseInt(time1.split(":").join(""));
var iTime2=parseInt(time2.split(":").join(""));
if (iTime1>iTime2) alert('error !!!');


Accepted Solution

Ichitaka earned 80 total points
ID: 8119264
Use this:

if (dateA.getTime() > dateB.getTaime()){
 alert("A > B");

N.B. If dateA & dateB aren't date object, make it date with:

var dateA = new Date("01/01/2000 01:00 PM");
var dateB = new Date("01/01/2000 11:00 AM");

Author Comment

ID: 9143800
Dear guys,

so sorry, haven't been logging onto the Experts-Exchange... Was very busy with projects at hand.

Anyway, either of your code worked: it's excellent piece of code, thanks!!!

Let me see if can spilt into points for u both, otherwise, have to make a decision on whose code is... slightly better.. Hmm

Best Regards,

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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

571 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