We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Dates and Military time validation needed

Westside2004
Westside2004 asked
on
Medium Priority
414 Views
Last Modified: 2008-02-01
Hi,

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?

Thx

-ws
Comment
Watch Question

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");
}
IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
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?

<script>
aDay=86400000
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');
    theForm.date1.focus()
    return false
  }
 return true
}
</script>
<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">
</form>


Michel

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
I think this would be simpler:

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

var temp = new Date(d1);
temp.setDate(d1.getDate()+1);
if(temp>d2) return false;
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
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) ) {
.
.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.