JavaScript - Add a Year to Current date - Asked By Sathish N on 07-Oct-08 03:09 AM

Hi!

              I want to Add a Year to Current Date in JavaScript ,The Date Select from Calendar Control (using Ajax).

Thanks

date - sri sri replied to Sathish N on 07-Oct-08 03:13 AM

hi,
try the following script to add year using javascript

<script language="javascript">
var myDate = new Date();
alert(myDate.getYear() + 1);
</script>

Hi - Sathish N replied to sri sri on 07-Oct-08 03:15 AM

Hi Sri!

             Thanks for your quick reply but i want to pass the values and Add year to the current Date.

Thanks

re - sowjanya ML replied to Sathish N on 07-Oct-08 03:19 AM

var today = new Date();
today.setDate(today.getDate() + numberOfDaysToAdd);
__________________
re - Web Star replied to Sathish N on 07-Oct-08 03:21 AM

function addDate()

{

var totalDate = new Date() ;

totalDate.setDate(totalDate.getDate() + 365) ;

alert(totalDate)

solution - Perry replied to Sathish N on 07-Oct-08 03:22 AM

I am paasing value = 90 so that it will add 90 to the current date.

<html>
<head>
<title>ADD datetitle>
<script>
function addDays(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(){addDays(document.forms[0].EffectiveDate,'90Days',90)};
</script>
</head>
<body>
<form>
<input type=text name="EffectiveDate" value="08/28/2005" size="10" onChange="addDays(this,'90Days',90)">
<input type=text name="90Days" size="10">
</form>
</body>
</html>

Please let me know if this helps.

Regards,

Megha

re - Web Star replied to Sathish N on 07-Oct-08 03:24 AM
the example below we set a Date object to a specific date (14th January 2010):
var myDate=new Date();
myDate.setFullYear(2010,0,14);

And in the following example we set a Date object to be 365 days into the future:

var myDate=new Date();
myDate.setDate(myDate.getDate()+365);
Hi - Sathish N replied to Perry on 07-Oct-08 03:29 AM

Hi Megha!

                  I am passing one Date that date will add one year as default.

Thanks

reply - Perry replied to Sathish N on 07-Oct-08 03:36 AM

If you see my program passing both the values date and number of days needs to be added. Pass your date and put number of days needs to be added is 1.

Regards,

Megha

try this - sri sri replied to Sathish N on 07-Oct-08 04:38 AM
hi,
try the below code

<script>
function DateAdd(para)
{
var myDate = new Date(para);
NextYear = myDate.getYear()+ 1
alert( myDate.getMonth()+1 + "/" +myDate.getDate() + "/" + NextYear);
}
</script>



Pass the date to the function
Add/Substract Day/Month/Year to a Date - C_A P replied to Sathish N on 07-Oct-08 06:22 AM

Add/Substract Day/Month/Year to a Date

add() and roll() are used to add or substract values to a Calendar object.

You specify which Calendar field is to be affected by the operation (Calendar.YEAR, Calendar.MONTH, Calendar.DATE).

add() adds or substracts values to the specified Calendar field, the next larger field is modified when the result makes the Calendar "rolls over".

String DATE_FORMAT = "yyyy-MM-dd";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
Calendar c1 = Calendar.getInstance();
c1.set(1999, 0 , 20); // 1999 jan 20
System.out.println("Date is : " + sdf.format(c1.getTime()));
c1.add(Calendar.DATE,20);
System.out.println("Date + 20 days is : " + sdf.format(c1.getTime()));
To substract, simply use a negative argument.

roll() does the same thing except you specify if you want to roll up (add 1) or roll down (substract 1) to the specified Calendar field. The operation only affects the specified field while add() adjusts other Calendar fields. See the following example, roll() makes january rolls to december in the same year while add() substract the YEAR field for the correct result.

String DATE_FORMAT = "yyyy-MM-dd";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
Calendar c1 = Calendar.getInstance();
// roll down the month
c1.set(1999, 0 , 20); // 1999 jan 20
System.out.println("Date is : " + sdf.format(c1.getTime()));
c1.roll(Calendar.MONTH, false); // roll down, substract 1 month
System.out.println
("Date roll down 1 month : "
+ sdf.format(c1.getTime())); // 1999 jan 20

c1.set(1999, 0 , 20); // 1999 jan 20
System.out.println("Date is : " + sdf.format(c1.getTime()));
c1.add(Calendar.MONTH, -1); // substract 1 month
System.out.println
("Date minus 1 month : "
+ sdf.format(c1.getTime())); // 1998 dec 20

Add Date - C_A P replied to Sathish N on 07-Oct-08 06:25 AM

example of your question

The method that done this process is the Calendar.add(int field, int amount). Where the value of the field can be Calendar.DATE, Calendar.MONTH, Calendar.YEAR. So this mean if you want to substract in days, months or years use Calendar.DATE, Calendar.MONTH or Calendar.YEAR respectively.

 
import java.util.http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html;
 
public class CalendarAddExample
{
public static void main(http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html[] args)
{
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html cal = http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html.getInstance();
 
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html.out.println("Today : " + cal.getTime());
 
// Substract 30 days from the calendar
cal.add(http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html.DATE, -30);
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html.out.println("30 days ago: " + cal.getTime());
 
// Add 10 months to the calendar
cal.add(http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html.MONTH, 10);
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html.out.println("10 months later: " + cal.getTime());
 
// Substract 1 year from the calendar
cal.add(http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html.YEAR, -1)
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html.out.println("1 year ago: " + cal.getTime());
}
}
 
 

In the code above we want to know what is the date back to 30 days ago.

The sample result of the code is shown below:

Today : Tue Jan 03 06:53:03 ICT 2006
30 days ago: Sun Dec 04 06:53:03 ICT 2005
REply - Binny ch replied to Sathish N on 07-Oct-08 01:10 PM
<!DOCTYPE HTML PUBLIC "-//W3C//http://webdesign.ittoolbox.com/groups/technical-functional/javascript-l/adding-days-to-date-in-javascript-1711140# 4.0 Transitional//EN">
<html>
<head>

<title>Untitled</title>
</head>
<body>
<script type="text/http://webdesign.ittoolbox.com/groups/technical-functional/javascript-l/adding-days-to-date-in-javascript-1711140#">
var userMonth
var userDate
var userYear
var daysAdded
var totalDate
var added
var month, date, year
function userDate()
{
<!--get user input-->
var userMonth = document.getElementById('month')
var userDate = document.getElementById('date')
var userYear = document.getElementById('year')
<!--ask user to check date entered-->
alert("You entered: " + userMonth.value + "/" + userDate.value + "/" + userYear.value)
<!--validate month-->
if ((isNaN(userMonth.value)) || userMonth.value > 12 || userMonth.value < 1)
{
alert("Please enter a month between 1 and 12")
document.userStartDate.month.focus()
}
<!--validate date-->
if ((isNaN(userDate.value)) || userDate.value > 31 || userDate.value < 1)
{
alert("Please enter a date between 1 and 31")
document.userStartDate.date.focus()
}
<!--validate year-->
if ((isNaN(userYear.value)) || userYear.value == "")
{
alert("Please enter a numerical year.")
document.userStartDate.year.focus()
}
}
function calcDate()
{
var daysAdded = document.getElementById('added')
var userMonth = document.getElementById('month')
var userDate = document.getElementById('date')
var userYear = document.getElementById('year')
<!--validate number added-->
if (isNaN(daysAdded.value))
{
alert("Please enter numbers only.")
document.addDate.added.focus()
}
<!--set the value of the user date-->
var totalDate = new Date()
totalDate.setYear(userYear.value)
totalDate.setMonth(userMonth.value-1)
totalDate.setDate(userDate.value)
<!--check the input-->
alert("userYear"+userYear.value + "userMonth"+userMonth.value + "userDate"+userDate.value)
<!--calculate the new date-->
totalDate.setDate(totalDate.getDate() + daysAdded.value)
alert(totalDate)
}

</script>
<!--form to get the user start date-->
<form name="userStartDate">
<h5>START:<br /> Mon: <input type="text" id="month" maxlength="2" size="2">
Date: <input type="text" id="date" maxlength="2" size="2">
Yr:<input type="text" id="year" maxlength="4" size="4"></h5>
<input type="button" onclick='userDate()' value="Confirm Date">
</form>

<!--form to get the number of days to add-->
<form name="userDaysAdded">
<h5>Days to Add: <input type="text" id="added"></h5>
<input type="button" onclick='calcDate()' value="Add Days">
</form>

</body>
</html>
Repyl - Binny ch replied to Sathish N on 07-Oct-08 01:12 PM
<html>
<head>
<title>Zvonko &#42;</title>
<script>
function addDays(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(){addDays(document.forms[0].EffectiveDate,'365Days',365)};
</script>
</head>
<body>
<form>
<input type=text name="EffectiveDate" value="08/28/2005" size="10" onChange="addDays(this,'90Days',90)">
<input type=text name="365Days" size="10">
</form>
</body>
print the date in dd/mm/yyyy format - Santosh Satish replied to Binny ch on 16-Apr-09 03:41 AM
hi, this code was very usefull to me but i want to print the date in dd/mm/yyyy format. how do it do it. Pls somebody help
I want to Add a Year to Current Date in JavaScript ,The Date Select from Calendar Control (using Ajax).
Ravi Gupta replied to Sathish N on 28-Apr-09 06:46 AM
end of post