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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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

752 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