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 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2003
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    add days to date entered in text box

    I want to create a function that will take a date that has been entered in to text box 1 and a number of days.

    The function should take the date value passed in from text box 1 convert it from a string to a date format and add the number of days passed in to the function to the date then output that value in the format dd/mm/yyyy to a second text box.

    So far I have:

    Text box that fires the function onBlur
    <input type="text" name="txt_ad_date" value ="" onBlur="addDays(26/01/2006, 1)">


    The function
    function addDays(myDate, days) {
    adDate = new Date(myDate);
    adDate.setDate(adDate.getDate() + days);
    document.frmPermVacReq.txt_closing_date.value = adDate;
    }

    This should result in
    07/02/2006 in text box 2 (txt_closing_date)

    what I actually get is Fri Jan 2 00:00:00 UTC 1970

    Any Ideas???

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,468
    Thanks
    3
    Thanked 495 Times in 482 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    </head>
    <script>
    
    
    
    function addDays(myDate, days) {
    adDate = new Date(myDate[0],myDate[1],(myDate[2]+days));
    document.frmPermVacReq.txt_closing_date.value = Format(adDate.getDate())+'/'+Format(adDate.getMonth()+1)+'/'+adDate.getFullYear();
    }
    
    function Format(nu){
     if (nu<10){ nu='0'+nu; }
     return nu;
    }
    </script>
    <body onload="addDays([2005,2,1], 4);">
    <form name="frmPermVacReq" >
    <input name="txt_closing_date" >
    </form>
    </body>
    
    </html>

  • #3
    New Coder
    Join Date
    Feb 2003
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Genius!!!

    Thank you.

    ammended the code slightly

    <input type="text" name="txt_ad_date" value ="" onBlur="document.frmPermVacReq.txt_closing_date.value = addDays(document.frmPermVacReq.txt_ad_date.value, 14)">

    function addDays(s, days) {
    days=(document.frmPermVacReq.txt_ad_date.value*1)//-1
    var dateArray = s.split('/');
    sdate = new Date(dateArray[2],dateArray[1]-1,dateArray[0]);

    if(dateArray[2].length<4){
    alert("Please Enter The Year As A Four Digit Number\n\nExample:- 2002\n\nThank You")
    return document.frmPermVacReq.txt_closing_date.value=""

    }else{

    var odate = new Date(sdate.getTime() + (days * 86400000));

    alert(sdate.getDate());
    alert(sdate.getMonth() + 1);
    alert(sdate.getYear());

    return odate.getDate() + '/' + (odate.getMonth()+1) + '/' + odate.getYear();
    }
    }

    Could not have done this with out your help spent ages on google. Thank you.


  •  

    Posting Permissions

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