Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    133
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Checking if a date is earlier than today

    Hi,

    I have the following function which compares the two dates coming from a form. (it fills the day,month,year dropdowns and gets the values from there to compare) I want the script also check if the check-in date (starting) is earlier than today.
    I tried to find some scripts on the internet to make it but didn't work.
    I'll be glad if anyone can help.

    Thanks
    telmessos


    Here is the JS file (Makes the comparison in calDuration() function.)


    function startFunction()
    {
    var monthtext=['01','02','03','04','05','06','07','08','09','10','11','12'];
    var today=new Date()
    dayfield=document.getElementById("day_start");
    monthfield=document.getElementById("month_start");
    yearfield=document.getElementById("year_start");

    var this_month=today.getMonth();
    var this_year=today.getFullYear();
    var days_in_month=32 - new Date(this_year, this_month, 32).getDate();

    for (var i=0; i<days_in_month; i++)
    dayfield.options[i]=new Option(i+1, i+1)
    dayfield.options[today.getDate()-1]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
    for (var m=0; m<12; m++)
    monthfield.options[m]=new Option(monthtext[m], monthtext[m])
    monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
    var thisyear=today.getFullYear()
    for (var y=0; y<2; y++)
    {
    yearfield.options[y]=new Option(thisyear, thisyear)
    thisyear+=1
    }
    yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
    }
    function endFunction()
    {
    var monthtext=['01','02','03','04','05','06','07','08','09','10','11','12'];
    var myDate=new Date()
    myDate.setDate(myDate.getDate()+1); //Initialized to 30 days in the future.

    dayfield=document.getElementById("day_end");
    monthfield=document.getElementById("month_end");
    yearfield=document.getElementById("year_end");

    var this_month=myDate.getMonth();
    var this_year=myDate.getFullYear();
    var days_in_month=32 - new Date(this_year, this_month, 32).getDate();

    for (var i=0; i<days_in_month; i++)
    dayfield.options[i]=new Option(i+1, i+1)
    dayfield.options[myDate.getDate()-1]=new Option(myDate.getDate(), myDate.getDate(), true, true) //select the day
    for (var m=0; m<12; m++)
    monthfield.options[m]=new Option(monthtext[m], monthtext[m])
    monthfield.options[myDate.getMonth()]=new Option(monthtext[myDate.getMonth()], monthtext[myDate.getMonth()], true, true) //select today's month
    var thisyear=myDate.getFullYear()
    for (var y=0; y<2; y++)
    {
    yearfield.options[y]=new Option(thisyear, thisyear)
    thisyear+=1
    }
    yearfield.options[0]=new Option(myDate.getFullYear(), myDate.getFullYear(), true, true) //select today's year
    }
    function month1Change()
    {
    var today=new Date();

    dayfield=document.dates.day_start;
    monthfield=document.getElementById("month_start");
    yearfield=document.getElementById("year_start");

    var this_day=10;
    var this_month=document.getElementById("month_start").selectedIndex;
    var this_year=document.getElementById("year_start").selectedIndex;

    var starting=new Date(this_year,this_month,this_day);
    var this_month=starting.getMonth();
    var this_year=starting.getFullYear();
    this_year=this_year+3;
    var days_in_month=32 - new Date(this_year, this_month, 32).getDate();

    var num=days_in_month-1; //Just to select the last date of the selected month.

    dayfield.options.length=0;
    for (var i=0; i<days_in_month; i++)
    dayfield.options[i]=new Option(i+1, i+1)
    dayfield.options[num]=new Option(days_in_month, days_in_month, true, true);
    }
    function month2Change()
    {
    var today=new Date();

    dayfield=document.dates.day_end;
    monthfield=document.getElementById("month_end");
    yearfield=document.getElementById("year_end");

    var this_day=10;
    var this_month=document.getElementById("month_end").selectedIndex;
    var this_year=document.getElementById("year_end").selectedIndex;

    var starting=new Date(this_year,this_month,this_day);
    var this_month=starting.getMonth();
    var this_year=starting.getFullYear();
    this_year=this_year+3;
    var days_in_month=32 - new Date(this_year, this_month, 32).getDate();
    var num=days_in_month-1; //Initiated the second date only to the last date of the month.

    dayfield.options.length=0;

    for (var i=0; i<days_in_month; i++)
    dayfield.options[i]=new Option(i+1, i+1)
    dayfield.options[num]=new Option(days_in_month, days_in_month, true, true);
    }
    function calDuration()
    {
    var cal_duration=new Date();
    var one_day=1000*60*60*24;
    start_day=document.getElementById("day_start").value;
    start_month=document.getElementById("month_start").selectedIndex;
    start_month=start_month-1;
    start_year=document.getElementById("year_start").value;

    end_day=document.getElementById("day_end").value;
    end_month=document.getElementById("month_end").selectedIndex;
    end_month=end_month-1;
    end_year=document.getElementById("year_end").value;

    var ending=new Date(end_year,end_month,end_day);
    var starting=new Date(start_year,start_month,start_day);
    var diff=Math.floor((ending.getTime()-starting.getTime())/(one_day));

    if(diff<=0){
    alert("Departure date can't be earlier than Arrival date");
    startFunction();
    return false;
    }
    return true;
    /*else
    startRequest();*/ //This is actually for an xmlHttp call. Ignore it.
    }

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Too many lines of code.

    Code:
    function calDuration()
    {
    ...
    	start_day=document.getElementById("day_start").value;
    	start_month=document.getElementById("month_start").selectedIndex;
    	start_month=start_month-1;
    	start_year=document.getElementById("year_start").value;
    	
    	end_day=document.getElementById("day_end").value;
    	end_month=document.getElementById("month_end").selectedIndex;
    	end_month=end_month-1;
    	end_year=document.getElementById("year_end").value;
    	
    	/*******/
    	var ending=new Date(end_year,end_month,end_day);
    	var starting=new Date(start_year,start_month,start_day);
    	var diff=Math.floor((ending.getTime()-starting.getTime())/(one_day));
    	
    	if(diff<=0){
    		alert("Departure date can't be earlier than Arrival date");
    		startFunction();
    		return false;
    	}
    	/*******/
    	return true;
    	/*else
    		startRequest();*/		//This is actually for an xmlHttp call. Ignore it.
    }
    The stuff above wrapped in comment lines... why are you doing all of that? You only want to make sure the end date is >= start date, right? And the date components are already handily stored in variables, so...
    Code:
    if (end_year >= start_year)
    {
      // year is ok
      if (end_month >= start_month)
      {
        // month is ok
        if (end_day >= start_day)
        {
          // day is ok
          return true;
        }
        else
        {
          alert("something");
          return false;
        }
      }
      else
      {
        alert("something");
        return false;
      }
    }
    else
    {
      alert("something");
      return false;
    }
    That can also be combined into a single if statement.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,315
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Code:
    <script type = "text/javascript">
    
    function checkDate() {
    
    var end_year = 2008;
    var end_month = 5;  // month 0 -11
    var end_day = 11;
    
    var now = new Date().getTime();
    var d = new Date();
    d.setFullYear(end_year, end_month, end_day);  // YYYY,MM(0-11),DD
    var selectedDate = d.getTime();
    if (selectedDate > (now - (1000 * 60 * 60 * 24))) {
    alert ("Date must be at least one day before today's date!");
    return false;
    }
    return true;
    }
    
    </script>
    At least once per year, some group of scientists will become very excited and announce that whatever they announced last year about global warming is wrong.
    Last edited by Philip M; 06-11-2008 at 09:23 AM.

  • Users who have thanked Philip M for this post:

    meenakshia (06-11-2008)

  • #4
    Regular Coder
    Join Date
    Apr 2006
    Posts
    133
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I am not good at Javascript. This script code is ready and working without any problems to check if the start date is later than the end date. What I need is adding another check, to find out if the start date is earlier than today. Philip M, I couldn't adopt your code to my existing code. I have no idea how to do it.
    Please help

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,315
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Just add the code and call the function

    checkdate()

    at the appropriate point.

    Your code is so long-winded and convoluted that I cannot easily see the best position for it. But you seem to be trying to run before you can walk. If you don't understand Javascript it is not too surprising that you find it hard to implement.

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by telmessos View Post
    What I need is adding another check, to find out if the start date is earlier than today.
    Sorry about that. I mis-read your original post.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •