C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » JavaScript »

How to add year or months from a given date using javascript


Posted Date: 29 Nov 2008      Posted By: Christopher      Member Level: Gold     Points: 1   Responses: 3




I have two textboxes ,First one i select current Date from "CalenderExtender" ,After that, In TextChanged Event I want ,exactly add one year,Please give code in javascript , Or please tell me simple way





Responses

Author: Ansar    30 Nov 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 6

<html>
<head>
<script>
function addYear(theField,fName,daysToAdd){
var theForm = theField.form;
var fDate = new Date(theField.value);
fDate.setDate(fDate.getDate()+daysToAdd);
var MM = fDate.getMonth()+1;
var DD = fDate.getDate();
var YY = fDate.getFullYear();
if(MM<10) MM="0"+MM;
if(DD<10) DD="0"+DD;
theForm[fName].value = MM+"/"+DD+"/"+YY;
}
onload=function(){addYear(document.forms[0].Today,'NextYear',365)};
</script>
</head>
<body>
<form>
<input type=text name="Today" value="12/31/2008" size="10">
<input type=text name="NextYear" size="10">
</form>
</body>



Author: satheesh    30 Nov 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 6

hey look at the following program in which you can chenge the year for every clicking event of the button take a look might use ful to you ...

<html>
<head>

<script language="javascript">
var curdate = new Date("12/11/2008");

function changeDate() {
document.form1.text1.value = ""+curdate;
curdate.setDate(curdate.getDate()+365);
var mon = curdate.getMonth();
var date = curdate.getDate();
var year = curdate.getFullYear();
if(mon<10) mon="0"+mon;
if(date<10) date="0"+date;
document.form1.text2.value = mon+"/"+date+"/"+year;
}

</script>
</head>

<body>
<form name="form1" method="get">
<input type="text" name="text1" value=""/>
<input type="text" name="text2" value=""/>
<br/>
<input type="button" name="btn1" value="Get Date" onclick="changeDate()"/>
</form>
</body>
</html>



Author: Deepika Haridas    30 Nov 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 5

<html>
<head>
<script>
function addYear(theField,fName,daysToAdd){
var theForm = theField.form;
var fDate = new Date(theField.value);
fDate.setDate(fDate.getDate()+daysToAdd);
var MM = fDate.getMonth()+1;
var DD = fDate.getDate();
var YY = fDate.getFullYear();
if(MM<10) MM="0"+MM;
if(DD<10) DD="0"+DD;
theForm[fName].value = MM+"/"+DD+"/"+YY;
}
onload=function(){addYear(document.forms[0].Today,'NextYear',365)};
</script>
</head>
<body>
<form>
<input type=text name="Today" value="12/31/2008" size="10">
<input type=text name="NextYear" size="10">
</form>
</body>



Thanks & Regards,
Deepika
Editor

If U want to shine like a SUN..First U have to burn like the SUN!!
Need a Guide? Join my mentor program..



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : How to use javascript validation in serverside controls?
Previous : Blank space validation
Return to Discussion Forum
Post New Message
Category: JavaScript

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use