Dates and Military time validation needed

Posted on 2006-03-27
Last Modified: 2008-02-01

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?


Question by:Westside2004
    LVL 6

    Expert Comment

    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");
    LVL 75

    Accepted Solution

    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">

    LVL 3

    Expert Comment

    I think this would be simpler:

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

    var temp = new Date(d1);
    if(temp>d2) return false;
    LVL 75

    Expert Comment

    by:Michel Plungjan
    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) ) {

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Both jQuery and Mootools offer some nice, easy to use scripts. Mootools is best known for their interactive slideshows and jQuery for their menus, but both have many other uses as well.   jQuery is basically a toolkit with many DOM (http://en.wik…
    When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (, typical array handling might look like this: (CODE) B…
    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…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now