Dates and Military time validation needed


I have two fields with dates and times in the format:

03/20/2006 16:07 (military times)

I need to ensure the following

That field 1 cannot be more than one day earlier than field 2.

How can I do this using JavaScript?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Where elem1 is the id of the first date/time field and and elem2 is the id of second date/time field. Should return true if the difference between the two dates is greater than a day by even a couple minutes.

function showDateDiff(elem1, elem2)
     var d1 = new Date(Date.parse(document.getElementById(elem1).value));
     var d2 = new Date(Date.parse(document.getElementById(elem2).value));
     var diff = (d1-d2)/86400000;

     if (Math.abs(diff) > 1) alert("true");
Michel PlungjanIT ExpertCommented:
No need to date.parse that string: javascript:alert(new Date('03/20/2006 16:07'))
also it was earlier, not different. Also why use the id, when all fields must have names and can be passed to the function as objects?

function showDateDiff(strDate1,strDate2) {
  var d1 = new Date(strDate1); // assuming the string is a valid date
  var d2 = new Date(strDate2);
  return (d2-d1);

function validate(theForm) {
  if (showDateDiff(theForm.date1.value,theForm.date2.value) > aDay) {
    alert('Date 1 is more than 24 hours earlier than Date 2');
    return false
 return true
<form onSubmit="return validate(this)">
<input type="text" name="date1" value="03/20/2006 16:07">
<input type="text" name="date2" value="03/21/2006 16:08">
<input type="submit">


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I think this would be simpler:

var d1= //field1
var d2= //field2

var temp = new Date(d1);
if(temp>d2) return false;
Michel PlungjanIT ExpertCommented:
Yes it would - if you made a date of d2 too.

More way to skin a cat:

aDay = 86400000;
function isMoreThanADay(strDate1,strDate2) {
  var d1 = new Date(strDate1).getTime();
  var d2 = new Date(strDate2).getTime();
  return (d2-d1)>aDay
function validate(theForm) {
  if (isMoreThanADay(theForm.date1.value,theForm.date2.value) ) {
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.