Calendarfunction with two dates. I want the selection of the first date to also set the second date one day later.

Posted on 2005-04-11
Last Modified: 2012-05-05
I have a page with two dates, an arrival date and a departure date. You can click the calendar-icon next to the datefields to set a date. What I want is to, when you set the arrival date, automatically set the departure date to one day later than the arrival date. However, if the departure date already is greater than arrival date then the departure date should not be changed.

Here is the page:
Question by:jenmat
    LVL 2

    Accepted Solution

    add this function to the top:

    function populate(aSource){
      var dest = document.forms[0].BookEndDate;
      if (aSource.value=="") return;
      var dtSource = getDateFromFormat(aSource.value,"yyyy-MM-dd");  
      if (dest.value==""){
        dest.value=formatDate(new Date(dtSource+24*60*60*1000),"yyyy-MM-dd");
      } else {  
        var dtDest=getDateFromFormat(dest.value,"yyyy-MM-dd");    
        if (dtDest<dtSource) {
          dest.value=formatDate(new Date(dtSource+24*60*60*1000),"yyyy-MM-dd");

    change the onblur function:

    name="BookStartDate" maxlength="10" value="2005-04-15" onblur="populate(this,this.form.BookEndDate)"

    onkeyup="this.value=this.value.split(' ').join('');" class="FormFieldM" type="text">

    I also notice that your form declaration is incorrect in such that you've closed the form at the very start of the page, with all other elements placed outside the form, You should move your </form> tag to the end of your page.

    Author Comment

    This sort of works.

    The problem is that the onblur function only works when i step from the field BookStartDate to another field and not just by selecting a date in the calendar window.  Is there another way to call the function than onblur?
    LVL 2

    Expert Comment

    Not a very clean solution, but you may try this:

    add the function:

    function checkDates(){


    <body onload="checkDates()">

    remove the onblur declaration placed previously.
    LVL 2

    Expert Comment

    If you want to have a cleaner solution, you would have to modify your DevCalendar.js and change the CalendarPopup_tmpReturnFunction function to call populate before returning.

    You may also need to add in a few custom properties to the CalendarPopup object so your tmpReturnFunction would not need to hardcode the parameters to be passed to populate.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    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…

    761 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

    11 Experts available now in Live!

    Get 1:1 Help Now