Click here to Skip to main content
15,886,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have two datepicker text boxes (jquery ui).
The req is when I select a particular date on textbox1, the other textbox automatically populates 3 days prior to the date selected on textbox1 (javascript).

Right now I am using the below function:

C#
function getdate() {
        var tt = document.getElementById('txtDate').value;
       
        

         date      = new Date(tt);
            next_date = new Date(date.setDate(date.getDate() + 1));
            formatted = padNumber(next_date.getUTCMonth() + 1) + '/' + padNumber(next_date.getUTCDate() + 3) + '/' + next_date.getUTCFullYear()
            document.getElementById('follow_Date').value = formatted;
        }


But when the 30th day comes, it adds 30 + 3 and shows 33 instead of going to the next month.
Please help. :)
Posted
Updated 30-May-14 0:50am
v4
Comments
Thanks7872 30-May-14 6:32am    
And what is the question?
ujju.1 30-May-14 6:51am    
please see the updated qsn.
See my Answer. Solution 2.

Try jQuery:
XML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script></script>
 <script>
$(document).ready(function(){
    $( "#datepicker1" ).datepicker({
        onClose: function() {
            var date2 = $('#datepicker1').datepicker('getDate');
            date2.setDate(date2.getDate()+3)
            $( "#datepicker2" ).datepicker("setDate", date2);
        }
    });
    $( "#datepicker2" ).datepicker();
});
</script>
</head>
<body>
<p>Date1: <input type="text" id="datepicker1"></p>
<p>Date2: <input type="text" id="datepicker2"></p>
</body>
</html>
 
Share this answer
 
Comments
Naz_Firdouse 30-May-14 8:27am    
Nice solution
RDBurmon 30-May-14 11:31am    
perfect solution +5
Member 12395105 27-Apr-16 6:45am    
Its working thanq as lyk dis i want for next 28 days ,how to write for dat?
i wanna save in sql table also... hw to write plz help me reply to my mail kamalaveni.n.t@gmail.com

Solution


Use setDate()[^] function.
JavaScript
function getdate() {
    var tt = document.getElementById('txtDate').value;

    var date = new Date(tt);
    var newdate = new Date(date);

    newdate.setDate(newdate.getDate() + 3);

    var dd = newdate.getDate();
    var mm = newdate.getMonth() + 1;
    var y = newdate.getFullYear();

    var someFormattedDate = mm + '/' + dd + '/' + y;
    document.getElementById('follow_Date').value = someFormattedDate;
}

Demo


[Demo] Add Days to a Date in JavaScript[^]
 
Share this answer
 
Comments
Abhishek Pant 30-May-14 7:41am    
good solution..
Thanks Abhishek. :)
Abhishek Pant 30-May-14 8:56am    
The solution helped me too in one of my project.I tried to vote up but found bug on it[^].
Oh !!! That's great. :) Thanks.

I can see one upvote on my answer listed in my Reputation list. But I am not sure whether it is by you or not. Thanks for posting it as a Bug.
RDBurmon 30-May-14 11:30am    
good explanation
JavaScript
function getdate() {
        var tt = document.getElementById('txtDate').value;     
        
         date  = new Date(tt);
                date.today().add(3).days()
            document.getElementById('follow_Date').value =  date;
        }</pre>
 
Share this answer
 
Comments
RDBurmon 30-May-14 11:30am    
+3
This Is Might A Simplest Solution To Your Problem ..
..
XML
<script>
var d = new Date();
d.setDate(d.getDate() + 50);
document.getElementById("demo").innerHTML = d;
</script>

..
Thanks..:-)
 
Share this answer
 
Comments
Member 12395105 27-Apr-16 6:43am    
how select 28 days continously like this and save dat in sql- reply me plz to kamalaveni.n.t@gmail.com

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900