I have one text box. When I enter a date with the format MM/DD/YYYY
in that textbox and click outside the textbox I need to display the result as MM/DD/YYYY+3
using JavaScript.
For example, if my date is 12/31/2013 then the result would be 01/03/2014.
Hope this link will help you to find the answer:
Have a look at the excellent library moment.js with which you easily can add three days to your original date.
In your case it would be something like:
var input = moment(myTextBoxValue);
input.add('d', 3); // input is now 3 days later
I guess the easiest to program and best readable solution would be to use a dedicated library for handling dates, such as Moment.js .
There, you have got the add function which allows you to add an arbitrary amount of time to a given point in time. Eg:
moment().add('days', 7);
If you use the moment
function to parse the time entered, use that as your source value, call add
on it, and return it as JavaScript Date
using the toDate function , you get exactly what you want.
So basically it comes down to:
var sourceDate = moment(new Date(2013, 7, 8)),
targetDate = sourceDate.add('days', 3),
result = targetDate.toDate();
Then, result
contains the Date
object you wanted to have.
I'd prefer that over native JavaScript handling of Date
, as it is way more readble and hence understandable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.