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 2 of 2
  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
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Please try the following

    PHP Code:

    <script type="text/javascript">
    <!--
    function 
    calc_date() {
    s=document.f2.date_from.value
    days
    =(document.f2.daynums.value*1)//-1

    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.f2.date_to.value=""
    }
    else{
    odate = new Date(sdate.getTime() + (days 86400000));
    document.f2.date_to.value=odate.getDate() + '/' + (odate.getMonth()+1) + '/' odate.getYear()
    }
    }
    //-->
    </script>

    <form name="f2">
    <P><table border=1>
    <tr><td>Start Date</td><td>Number of days</td><td>End Date</td></tr>
    <tr><td><input type="text" name="date_from" size="10" value="24/01/2006"></td>
    <td align="center"><input type="text" name="daynums" size="4" maxlength="4" value="5"></td>
    <td><input type="text" name="date_to" size="10"></td></tr>
    <tr><td colspan=3 align=center>
    <input type="button" value="calculate" onclick="calc_date()">
    </td></tr></table>
    </form> 
    Last edited by Mr J; 01-24-2006 at 03:18 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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