Sharath
Sharath

Reputation: 185

Calculate the Date based on Current Date and No of Days using Javascript/Jquery

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.

Upvotes: 3

Views: 2752

Answers (4)

Andrew Squires
Andrew Squires

Reputation: 31

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());

Upvotes: 1

Rondel
Rondel

Reputation: 4951

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?

Upvotes: 0

Lars Andren
Lars Andren

Reputation: 8771

You could do something like

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

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

Upvotes: 0

RJ Regenold
RJ Regenold

Reputation: 1758

Date.js is fantastic for this.

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

Upvotes: 2

Related Questions