Add days to a Date and display in a text field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cptuser
    New Member
    • Mar 2007
    • 30

    Add days to a Date and display in a text field

    Hi,

    I'm a novice. I need to add a certain number of days to a date entered in a field (dd/mm/yyyy) and then display the calculated date in another field in the same format, dd/mm/yyyy. Here is what I have so far. I appreciate your help:

    Code:
    function calDate() {
    	
    var date1 = new Date(document.getElementById('date_field1').value);
    document.getElementById("date_field2").value = date1.setDate(date1.getDate()+5);
    
    }
    Many thanks.
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    [CODE=javascript]var myDate = new Date();

    //add a day to the date
    myDate.setDate( myDate.getDate( ) + 1);

    //add a week
    myDate.setDate( myDate.getDate( ) + 7);

    //add a month
    myDate.setMonth (myDate.getMont h() + 1);

    //add a year
    myDate.setYear( myDate.getYear( ) + 1);[/CODE]
    Last edited by acoder; Nov 27 '07, 10:23 AM. Reason: Added code tags

    Comment

    • cptuser
      New Member
      • Mar 2007
      • 30

      #3
      Hi,
      Sorry, but I'm afraid this does not help me at all. Can anyhow help with respect to my specific example and detailed explanation I provided.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Parse the date string using split(), then use setFullYear to set the date.

        To set the second field, use the getDate(), getMonth() and getFullYear() methods.

        See this reference.

        Comment

        • cptuser
          New Member
          • Mar 2007
          • 30

          #5
          Could you please kindly provide a full example based on my original explanation. This would be very helpful and I would learn quicker.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by cptuser
            Could you please kindly provide a full example based on my original explanation. This would be very helpful and I would learn quicker.
            You should be able to do this yourself with the information, but anyway here you go:
            [code=javascript]function calDate() {
            var dateArr = document.getEle mentById('date_ field1').value;
            var date1 = new Date();
            // assume entered in dd/mm/yyyy format - no validation check.
            date1.setFullYe ar(dateArr[2],dateArr[1]-1,dateArr[0]);
            date1.setDate(d ate1.getDate()+ 5);
            document.getEle mentById("date_ field2").value = date1.getDate() + "/" + (date1.getMonth ()+1) + "/" + date1.getFullYe ar();[/code]
            The month value is between 0 and 11 (which is why you need to add and subtract 1 when getting and setting).

            Comment

            • cptuser
              New Member
              • Mar 2007
              • 30

              #7
              Thank you very much! I did tried my best and spend quite a bit of time trying to work it out. As always you guys are absolutly terrific!!!! I tried the code provided, but I get "NaN/NaN/NaN" in the second date field. Is there a reason for that? I am entering a date in the format of dd/mm/yyyy in the first date field. Not sure where it's going wrong?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by cptuser
                I tried the code provided, but I get "NaN/NaN/NaN" in the second date field. Is there a reason for that?
                My fault for not testing. On line 5, you need to parse the strings using parseInt, e.g. parseInt(dateAr r[2]), parseInt(dateAr r[1])-1,...

                Comment

                • 13564
                  New Member
                  • Jan 2012
                  • 1

                  #9
                  still not working with parseInt, getting "NaN/NaN/NaN"

                  Comment

                  • robertybob
                    New Member
                    • Feb 2013
                    • 116

                    #10
                    Came across this thread so will complete the solution here for those still finding this in searches.

                    The problem is that the solution offered did not split the date entry to create the array used in the next line.

                    First line of the function should read
                    Code:
                    var dateArr = document.getElementById('date_field1').value.split('/');

                    Comment

                    Working...