3

I need a help.. I have a Current Date and No of days column. When i enter number of days,i should add current date plus no of days entered. For example, todays date 5th jan + 20(no of days) = 25th Jan 2011 in another column.

Kindly help me. Thanks in Advance.

Sharath
  • 185
  • 1
  • 2
  • 5
  • 1
    **[Read](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date)** – Madara's Ghost Jan 05 '12 at 15:32
  • Opposit to this http://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript ? – madflow Jan 05 '12 at 15:32
  • Have you thought about converting the known date to a timestamp via getTime(), adding the number of days in milliseconds to that number and making a new date from the result? – Wing Jan 05 '12 at 15:33
  • No.. I am learning Javascript and Jquery.. – Sharath Jan 05 '12 at 15:36
  • 2
    possible duplicate of [javascript date + 7 days](http://stackoverflow.com/questions/5741632/javascript-date-7-days) and many more... – Felix Kling Jan 05 '12 at 15:44

4 Answers4

2

Date.js is fantastic for this.

Date.today().add(5).days();
RJ Regenold
  • 1,748
  • 13
  • 17
1

As you are learning JavaScript you may find the w3schools site useful for simple examples of objects and functions that are exposed and how they may be used.

http://www.w3schools.com/jsref/jsref_obj_date.asp

You can calculate the date as follows:

var d = new Date(); // Gets current date
var day = 86400000; // # milliseconds in a day
var numberOfDays = 20;
d.setTime(d.getTime() + (day*numberOfDays)); // Add the number of days in milliseconds

You can then use one of the various methods of displaying the date:

alert(d.toUTCString());
0

You can add dates like this in js:

var someDate = new Date();
var numberOfDaysToAdd = 6;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd); 
var month = someDate.getMonth() + 1; //Add 1 because January is set to 0 and Dec is 11
var day = someDate.getDate();
var year = someDate.getFullYear();
document.write(month + "/" + day + "/" + year);

See this p.cambell's answer here: How to add number of days to today's date?

Community
  • 1
  • 1
Rondel
  • 4,811
  • 11
  • 41
  • 67
  • Actually i have two columns. Dynamically i have to get current date and number of days from those columns and then calculate the days and then show it in another column. – Sharath Jan 05 '12 at 15:43
0

You could do something like

Date.today().add(X).days();

Where X is the number of days the user has entered.

Lars Andren
  • 8,601
  • 7
  • 41
  • 56