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.
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.
Share Improve this question edited Aug 8, 2013 at 12:18 Cody Gray♦ 245k53 gold badges505 silver badges586 bronze badges asked Aug 8, 2013 at 11:49 AjeeshAjeesh 2872 silver badges8 bronze badges 1- Sorry, question is unclear – Ivan Chernykh Commented Aug 8, 2013 at 11:50
3 Answers
Reset to default 4Hope this link will help you to find the answer:
adding-number-of-days-to-an-entered-date-in-javascript.
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. E.g.:
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 es 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745391573a4625684.html
评论列表(0条)