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

x
?
Solved

Dates and Military time validation needed

Posted on 2006-03-27
4
Medium Priority
?
390 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
0
Comment
Question by:Westside2004
  • 2
4 Comments
 
LVL 6

Expert Comment

by:SirCrofty
ID: 16307965
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");
}
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1000 total points
ID: 16308421
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
0
 
LVL 3

Expert Comment

by:el_dios
ID: 16309048
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;
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16309748
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) ) {
.
.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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

580 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