admin管理员组文章数量:1426058
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.
本文标签: How to add three days to a given date in JavaScriptStack Overflow
版权声明:本文标题:How to add three days to a given date in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745391573a2656620.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论