Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text box in which i have entered some date i want in another text box in which i want to display the date after one week. i am using javascript function but it unfortunately and didn't solve my problem because the month returns by the function is one less than the current month.

Here is the code which i have written.




C#
<script type="text/javascript">
        function validNumber(e) {debugger;
            keyPressed = e.keyCode;
            if (keyPressed < 48 || keyPressed > 57) {
                return false;
            }

            return true;
        }
        function gettodate(todate) {
           // debugger;
            if (Validatetxt_fromdateformat()) {
                debugger;
                var fromDate = document.getElementById('txt_fromdate');
                var Month = fromDate.value.split("/")[0];
                var temp = new Array();
                temp = todate.split('/')
                var myDate = new Date(temp[2], temp[0], temp[1]);
                myDate.setDate(myDate.getDate() + 7);
                var curr_month = myDate.getMonth();
                if(Month < curr_month) {
                    curr_month--;
                }
                var curr_date = myDate.getDate();

                // if (curr_month == 0)
                //curr_month = curr_month + 1;
                //curr_month = curr_month + 1;
                if (curr_month == 0) {
                    curr_month = 12;
                }
                var curr_year = myDate.getFullYear();
                document.getElementById('txt_todate').value = curr_month + '/' + curr_date + '/' + curr_year;
            }
            else {
                document.getElementById('txt_todate').value = "";
            }
        }
        function Validatetxt_fromdateformat() {//debugger;
            var fromDate = document.getElementById('txt_fromdate');
            var toDate = document.getElementById('txt_todate');
            var Month = fromDate.value.split("/")[0];
            var date = fromDate.value.split("/")[1];
            var year = fromDate.value.split("/")[2];
            if ((Month < 0 || Month > 12) || (date < 1 || date > 31) || (year.length != 4)) {
                alert("Invalid date.Please enter correct Date(MM/DD/YYYY)");
                fromDate.focus();
                toDate.value = "";
                return false;
            }
            else
                return Month;
        }
        function Disabletodate() {debugger;
            var toDate = document.getElementById('ctl00_ContentPlaceHolder1_txt_todate');
            toDate.enabled = true;
        }






I need ur help...
Thanks in advance......
Posted
Updated 13-Dec-10 7:01am
v3
Comments
Abdul Quader Mamun 13-Dec-10 13:02pm    
Spelling check.

1 solution

You should simply remember that months go from 0 to 11 instead of 1 to 12, or add and subtract when converting from/to a date.

But have a look at this link. This will help you out:

http://slingfive.com/pages/code/jsDate/jsDate.html[^]

Good luck!
 
Share this answer
 
Comments
pk.pradeep 14-Dec-10 0:57am    
Thanks a lot this link is very helpful...
E.F. Nijboer 14-Dec-10 3:34am    
Nice :-) (also, please accept the answer if it helped)

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