Jquery 将月份添加到所选日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9380131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery add month to selected date
提问by Moksha
We have 1 Month, 3 Month, 6 Month, 9 Months, 1 Year Membership in dropdown list
我们在下拉列表中有 1 个月、3 个月、6 个月、9 个月、1 年的会员资格
What we where trying to do is if user select 25/06/2012 for start of membership of 9 months, we want to show him when his membership will expire like say 25/03/2013. we tried many things but it give some wrong date.
我们想要做的是,如果用户选择 25/06/2012 开始 9 个月的会员资格,我们想告诉他他的会员资格何时到期,比如 25/03/2013。我们尝试了很多东西,但它给出了一些错误的日期。
we are using jquery-ui for selection of start membership date
我们正在使用 jquery-ui 来选择开始会员日期
Regards Arshad
问候阿尔沙德
采纳答案by Diodeus - James MacFarlane
回答by Shyju
var date1 = new Date();
date1 .setMonth(date1 .getMonth() - 12);
now date1 is an object holding a date which is 12 months ago
现在 date1 是一个保存 12 个月前日期的对象
If you want to set it to the jQuery UI DatePickers, you can do this
如果你想把它设置为 jQuery UI DatePickers,你可以这样做
$("#txtToDate").datepicker("setDate", new Date());
$("#txtFromDate").datepicker("setDate", date1 );
回答by Bui Cuong
Try this:
尝试这个:
var s = $("#inputid");
var d= $.datepicker.parseDate(datepickeroption.dateFormat, s)
d.setMonth( d.getMonth( ) + 1 );